sinatra-log 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -1
- data/README.md +52 -12
- data/lib/sinatra/log.rb +1 -117
- data/lib/sinatra/log/default_formatter.rb +53 -0
- data/lib/sinatra/log/log.rb +60 -0
- data/lib/sinatra/log/version.rb +5 -0
- data/sinatra-log.gemspec +3 -3
- data/spec/formatter_spec.rb +1 -1
- data/spec/log_spec.rb +1 -1
- metadata +5 -5
- data/lib/sinatra/version.rb +0 -5
- data/spec/spec_helper.bak.rb +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4d659d2f552791227ac04e85055643571dd55165
|
4
|
+
data.tar.gz: 1aee0bd7348a25ad784cad47d3a2cc55f119e474
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 10896c174b8bd6b1742f59f6f96e67db3b846e87a5105f5ebd3208c44453b804508a0369516666e14d34d23f57d7c62a91f322ef82d7cc0ea0c0997844201ac3
|
7
|
+
data.tar.gz: 0c93f7e05bc8f3360203c3a0b4d74a1ec2f5b2c588d3c9fb3c7bc25623e38d5c6c097827282143f1b76877932a98e2cd82a66d5e51245aa9bd6e066bde0ec222
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,22 +1,62 @@
|
|
1
|
-
#
|
1
|
+
# Sinatra::Log ![Build Status][1]
|
2
2
|
|
3
|
-
|
3
|
+
A simple file logger written for use by Sinatra apps.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'sinatra-log'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle install
|
14
|
+
|
15
|
+
Or install it globally:
|
16
|
+
|
17
|
+
$ gem install sinatra-log
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```
|
22
|
+
class MyProject
|
23
|
+
def self.log
|
24
|
+
@logger ||= Sinatra::Log.new(:logger_name => 'myproject',
|
25
|
+
:log_filename => '/var/log/development.log',
|
26
|
+
:loglevel => 'WARN',
|
27
|
+
:enabled => true,
|
28
|
+
:project_dir => '/var/opt/myproj')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
MyProject.log.error "Error level log" # You will see this
|
33
|
+
MyProject.log.warn "Warning level log" # You will see this
|
34
|
+
MyProject.log.info "Info level log" # But not this
|
35
|
+
MyProject.log.debug "Debug level log" # or this
|
36
|
+
|
37
|
+
```
|
38
|
+
|
39
|
+
This will configure the logger to output messages at WARN level or higher to
|
40
|
+
be directed to the location /var/log/development.log. Logging is enabled and
|
41
|
+
the log4r logger is named 'myproject'.
|
42
|
+
|
43
|
+
Each log messages that is generated by log4r will contain the full path and
|
44
|
+
filename of the origin of the log message. Specifying the project root
|
45
|
+
directory in :project_dir, will cause that directory to be removed from each
|
46
|
+
log message.
|
47
|
+
|
48
|
+
## History
|
4
49
|
|
5
50
|
I wrote this wrapper around log4r in 2014 for a small Sinatra project at
|
6
51
|
[Lookout][4]. As the project ended, the code was pulled out
|
7
52
|
and open sourced by [ismith][3] and included in [lookout-rack-utils][2].
|
8
53
|
|
9
|
-
|
10
|
-
|
11
|
-
It is mostly unchanged here, though configuration is done through the logging
|
12
|
-
constructor, not through configatron. Graphite logging was also thrown out,
|
13
|
-
though that could be re-added if desired. And of course there was a namespace
|
14
|
-
change.
|
15
|
-
|
16
|
-
### Purpose
|
54
|
+
## Changes
|
17
55
|
|
18
|
-
|
19
|
-
|
56
|
+
The changes here remove the Lookout specific bits of the logging - the use of
|
57
|
+
specific configatron values that may not match your project or style.
|
58
|
+
Graphite logging was also thrown out, though that could be re-added if
|
59
|
+
desired. And of course there was a namespace change.
|
20
60
|
|
21
61
|
[1]: https://api.travis-ci.org/svrana/sinatra-log.svg?branch=master
|
22
62
|
[2]: https://github.com/lookout/lookout-rack-utils.git
|
data/lib/sinatra/log.rb
CHANGED
@@ -1,118 +1,2 @@
|
|
1
1
|
require 'rubygems'
|
2
|
-
require '
|
3
|
-
require 'time'
|
4
|
-
|
5
|
-
module Sinatra
|
6
|
-
# Logs to specified filename with the format:
|
7
|
-
#
|
8
|
-
# [Log Level]: [Timestamp (ISO-8601)]: [File:linenum]: [Log Message]
|
9
|
-
#
|
10
|
-
# Access is usually wrapped in a class helper:
|
11
|
-
#
|
12
|
-
# class MyProject
|
13
|
-
# self.log
|
14
|
-
# @logger ||= Sinatra::Log.new(:logger_name => 'myproject',
|
15
|
-
# :log_filename => 'myproject/dev.log',
|
16
|
-
# :loglevel => 'WARN',
|
17
|
-
# :enabled => true,
|
18
|
-
# :project_dir => '/var/opt/myproj')
|
19
|
-
# end
|
20
|
-
# end
|
21
|
-
class Log
|
22
|
-
include Log4r
|
23
|
-
|
24
|
-
REQUIRED_CONFIG_SYMBOLS = [:logger_name, :loglevel, :log_filename,
|
25
|
-
:enabled].freeze
|
26
|
-
|
27
|
-
# Formatter that include the filename and relative path, and line number in
|
28
|
-
# output of the caller.
|
29
|
-
#
|
30
|
-
# Since all callers go through the methods defined in this class to log, we
|
31
|
-
# look at the second line of the tracer output, removing everything but the
|
32
|
-
# directories after the project directory.
|
33
|
-
class DefaultFormatter < Log4r::Formatter
|
34
|
-
attr_reader :basedir
|
35
|
-
|
36
|
-
# @param [String] basedir The base project directory; this directory
|
37
|
-
# will be filtered out from each log entry if specified.
|
38
|
-
def initialize(basedir = nil)
|
39
|
-
super
|
40
|
-
@basedir = basedir
|
41
|
-
end
|
42
|
-
|
43
|
-
# Return a trimmed version of the filename from where a LogEvent occurred
|
44
|
-
#
|
45
|
-
# @param [String] tracer A line from the LogEvent#tracer Array
|
46
|
-
# @return [String] Trimmed and parsed version of the file ane line number
|
47
|
-
def event_filename(tracer)
|
48
|
-
if basedir.nil?
|
49
|
-
parts = tracer.match(/(.*:[0-9]+).*:/)
|
50
|
-
else
|
51
|
-
parts = tracer.match(/#{basedir}\/(.*:[0-9]+).*:/)
|
52
|
-
end
|
53
|
-
|
54
|
-
# If we get no matches back, we're probably in a jar file in which case
|
55
|
-
# the format of the tracer is going to be abbreviated
|
56
|
-
if parts.nil?
|
57
|
-
parts = tracer.match(/(.*:[0-9]+).*:/)
|
58
|
-
end
|
59
|
-
return parts[-1] if parts
|
60
|
-
end
|
61
|
-
|
62
|
-
# Receive the LogEvent and pull out the log message and format it for
|
63
|
-
# display in the logs
|
64
|
-
#
|
65
|
-
# @param [Log4r::LogEvent] event
|
66
|
-
# @return [String] Formatted log message
|
67
|
-
def format(event)
|
68
|
-
filename = event_filename(event.tracer[1])
|
69
|
-
time = Time.now.utc.iso8601
|
70
|
-
return "#{Log4r::LNAMES[event.level]}: #{time}: #{filename}: #{event.data}\n"
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
attr_reader :outputter
|
75
|
-
|
76
|
-
def initialize(config={})
|
77
|
-
errors = []
|
78
|
-
REQUIRED_CONFIG_SYMBOLS.each do |key|
|
79
|
-
if !config.include?(key)
|
80
|
-
errors << "#{key} required, but not specified in config hash"
|
81
|
-
end
|
82
|
-
end
|
83
|
-
raise ArgumentError, "#{errors}" if errors.count > 0
|
84
|
-
|
85
|
-
logger_name = config[:logger_name].to_s.gsub(/\s+/, '_')
|
86
|
-
@logger = Log4r::Logger.new(logger_name)
|
87
|
-
|
88
|
-
if config[:enabled]
|
89
|
-
index = Log4r::LNAMES.index(config[:loglevel])
|
90
|
-
# if logger.level is not in LNAMES an exception will be thrown
|
91
|
-
@logger.level = index unless index.nil?
|
92
|
-
else
|
93
|
-
@logger.level = Log4r::OFF
|
94
|
-
end
|
95
|
-
|
96
|
-
@outputter = FileOutputter.new("#{logger_name}fileoutput",
|
97
|
-
:filename => config[:log_filename],
|
98
|
-
:trunc => false)
|
99
|
-
@logger.trace = true
|
100
|
-
@outputter.formatter = DefaultFormatter.new(config[:project_dir])
|
101
|
-
@logger.outputters = @outputter
|
102
|
-
end
|
103
|
-
|
104
|
-
|
105
|
-
[:debug, :info, :warn, :error, :fatal, :level].each do |method|
|
106
|
-
define_method(method) do |*args, &block|
|
107
|
-
@logger.send(method, *args, &block)
|
108
|
-
end
|
109
|
-
|
110
|
-
# Returns true iff the current severity level allows for
|
111
|
-
# the printing of level messages.
|
112
|
-
allow_logging = "#{method}?".to_sym
|
113
|
-
define_method(allow_logging) do |*args|
|
114
|
-
@logger.send(allow_logging, *args)
|
115
|
-
end
|
116
|
-
end
|
117
|
-
end
|
118
|
-
end
|
2
|
+
require 'sinatra/log/log'
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'time'
|
2
|
+
require 'log4r'
|
3
|
+
|
4
|
+
module Sinatra
|
5
|
+
class Log
|
6
|
+
# Formatter that include the filename and relative path, and line number in
|
7
|
+
# output of the caller.
|
8
|
+
#
|
9
|
+
# Since all callers go through the methods defined in this class to log, we
|
10
|
+
# look at the second line of the tracer output, removing everything but the
|
11
|
+
# directories after the project directory.
|
12
|
+
class DefaultFormatter < Log4r::Formatter
|
13
|
+
attr_reader :basedir
|
14
|
+
|
15
|
+
# @param [String] basedir The base project directory; this directory
|
16
|
+
# will be filtered out from each log entry if specified.
|
17
|
+
def initialize(basedir = nil)
|
18
|
+
super
|
19
|
+
@basedir = basedir
|
20
|
+
end
|
21
|
+
|
22
|
+
# Return a trimmed version of the filename from where a LogEvent occurred
|
23
|
+
#
|
24
|
+
# @param [String] tracer A line from the LogEvent#tracer Array
|
25
|
+
# @return [String] Trimmed and parsed version of the file ane line number
|
26
|
+
def event_filename(tracer)
|
27
|
+
if basedir.nil?
|
28
|
+
parts = tracer.match(/(.*:[0-9]+).*:/)
|
29
|
+
else
|
30
|
+
parts = tracer.match(/#{basedir}\/(.*:[0-9]+).*:/)
|
31
|
+
end
|
32
|
+
|
33
|
+
# If we get no matches back, we're probably in a jar file in which case
|
34
|
+
# the format of the tracer is going to be abbreviated
|
35
|
+
if parts.nil?
|
36
|
+
parts = tracer.match(/(.*:[0-9]+).*:/)
|
37
|
+
end
|
38
|
+
return parts[-1] if parts
|
39
|
+
end
|
40
|
+
|
41
|
+
# Receive the LogEvent and pull out the log message and format it for
|
42
|
+
# display in the logs
|
43
|
+
#
|
44
|
+
# @param [Log4r::LogEvent] event
|
45
|
+
# @return [String] Formatted log message
|
46
|
+
def format(event)
|
47
|
+
filename = event_filename(event.tracer[1])
|
48
|
+
time = Time.now.utc.iso8601
|
49
|
+
return "#{Log4r::LNAMES[event.level]}: #{time}: #{filename}: #{event.data}\n"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'log4r'
|
3
|
+
require 'sinatra/log/default_formatter'
|
4
|
+
|
5
|
+
module Sinatra
|
6
|
+
# Logs to specified filename with the format:
|
7
|
+
#
|
8
|
+
# [Log Level]: [Timestamp (ISO-8601)]: [File:linenum]: [Log Message]
|
9
|
+
#
|
10
|
+
class Log
|
11
|
+
include Log4r
|
12
|
+
|
13
|
+
attr_reader :outputter
|
14
|
+
|
15
|
+
REQUIRED_CONFIG_SYMBOLS = [:logger_name, :loglevel, :log_filename,
|
16
|
+
:enabled].freeze
|
17
|
+
|
18
|
+
def initialize(config={})
|
19
|
+
errors = []
|
20
|
+
REQUIRED_CONFIG_SYMBOLS.each do |key|
|
21
|
+
if !config.include?(key)
|
22
|
+
errors << "#{key} required, but not specified in config hash"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
raise ArgumentError, "#{errors}" if errors.count > 0
|
26
|
+
|
27
|
+
logger_name = config[:logger_name].to_s.gsub(/\s+/, '_')
|
28
|
+
@logger = Log4r::Logger.new(logger_name)
|
29
|
+
|
30
|
+
if config[:enabled]
|
31
|
+
index = Log4r::LNAMES.index(config[:loglevel])
|
32
|
+
# if logger.level is not in LNAMES an exception will be thrown
|
33
|
+
@logger.level = index unless index.nil?
|
34
|
+
else
|
35
|
+
@logger.level = Log4r::OFF
|
36
|
+
end
|
37
|
+
|
38
|
+
@outputter = FileOutputter.new("#{logger_name}fileoutput",
|
39
|
+
:filename => config[:log_filename],
|
40
|
+
:trunc => false)
|
41
|
+
@logger.trace = true
|
42
|
+
@outputter.formatter = DefaultFormatter.new(config[:project_dir])
|
43
|
+
@logger.outputters = @outputter
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
[:debug, :info, :warn, :error, :fatal, :level].each do |method|
|
48
|
+
define_method(method) do |*args, &block|
|
49
|
+
@logger.send(method, *args, &block)
|
50
|
+
end
|
51
|
+
|
52
|
+
# Returns true iff the current severity level allows for
|
53
|
+
# the printing of level messages.
|
54
|
+
allow_logging = "#{method}?".to_sym
|
55
|
+
define_method(allow_logging) do |*args|
|
56
|
+
@logger.send(allow_logging, *args)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/sinatra-log.gemspec
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'sinatra/version'
|
4
|
+
require 'sinatra/log/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "sinatra-log"
|
8
|
-
spec.version = Sinatra::
|
8
|
+
spec.version = Sinatra::Log::VERSION
|
9
9
|
spec.authors = ["Shaw Vrana"]
|
10
10
|
spec.email = ["shaw@vranix.com1"]
|
11
|
-
spec.description = %q{A logger for Sinatra}
|
11
|
+
spec.description = %q{A logger for Sinatra applications}
|
12
12
|
spec.summary = %q{}
|
13
13
|
spec.homepage = "http://github.com/svrana/sinatra-log"
|
14
14
|
spec.license = "MIT"
|
data/spec/formatter_spec.rb
CHANGED
data/spec/log_spec.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-log
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shaw Vrana
|
@@ -94,7 +94,7 @@ dependencies:
|
|
94
94
|
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
|
-
description: A logger for Sinatra
|
97
|
+
description: A logger for Sinatra applications
|
98
98
|
email:
|
99
99
|
- shaw@vranix.com1
|
100
100
|
executables: []
|
@@ -109,11 +109,12 @@ files:
|
|
109
109
|
- README.md
|
110
110
|
- Rakefile
|
111
111
|
- lib/sinatra/log.rb
|
112
|
-
- lib/sinatra/
|
112
|
+
- lib/sinatra/log/default_formatter.rb
|
113
|
+
- lib/sinatra/log/log.rb
|
114
|
+
- lib/sinatra/log/version.rb
|
113
115
|
- sinatra-log.gemspec
|
114
116
|
- spec/formatter_spec.rb
|
115
117
|
- spec/log_spec.rb
|
116
|
-
- spec/spec_helper.bak.rb
|
117
118
|
- spec/spec_helper.rb
|
118
119
|
homepage: http://github.com/svrana/sinatra-log
|
119
120
|
licenses:
|
@@ -142,5 +143,4 @@ summary: ''
|
|
142
143
|
test_files:
|
143
144
|
- spec/formatter_spec.rb
|
144
145
|
- spec/log_spec.rb
|
145
|
-
- spec/spec_helper.bak.rb
|
146
146
|
- spec/spec_helper.rb
|
data/lib/sinatra/version.rb
DELETED