leafy-logger 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +4 -0
- data/Gemfile +9 -0
- data/LICENSE +22 -0
- data/README.md +122 -0
- data/Rakefile +14 -0
- data/leafy-logger.gemspec +32 -0
- data/lib/leafy-logger.rb +1 -0
- data/lib/leafy-logger_jars.rb +33 -0
- data/lib/leafy/logger.rb +1 -0
- data/lib/leafy/logger/appender_factories.rb +89 -0
- data/lib/leafy/logger/factory.rb +112 -0
- data/lib/leafy/logger/version.rb +6 -0
- data/spec/appender_factories_spec.rb +147 -0
- data/spec/factory_spec.rb +155 -0
- data/spec/logging.yml +16 -0
- data/spec/setup.rb +2 -0
- metadata +133 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f4f12c6c983804cb48d0172d68da27e018c0e91e
|
4
|
+
data.tar.gz: 4daaa5f43e2b6c1df82ba7aa13d525c03ca77f40
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e706a211dfec989155f3634142a6c8f16f9ca1766ca09ad0f185bfeb6451cc45af30719cc4cefcd81dc594441ea1be418150ca8f3ac1968bc9ae0599897792be
|
7
|
+
data.tar.gz: d37d627a59ba10ffc595f750411a64d05cabd56c773e440cecc92f32dd414452e5b20ebd2adfe462c60a217827dcf2808cad135c8b8dca23b79f973027537734
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Lookout
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
# Leafy-Logger
|
2
|
+
|
3
|
+
## installation
|
4
|
+
|
5
|
+
via rubygems
|
6
|
+
```
|
7
|
+
gem install leafy-logger
|
8
|
+
```
|
9
|
+
or add to your Gemfile
|
10
|
+
```
|
11
|
+
gem 'leafy-logger
|
12
|
+
```
|
13
|
+
|
14
|
+
installing the gem also takes care of the jar dependencies with jruby-1.7.16+
|
15
|
+
|
16
|
+
## usage
|
17
|
+
|
18
|
+
default configuration
|
19
|
+
```
|
20
|
+
require 'leafy/logger/factory'
|
21
|
+
require 'leafy/metrics/registry'
|
22
|
+
|
23
|
+
f = Leafy::Logger::Factory.new
|
24
|
+
f.configure( Leafy::Metrics::Registry.new, 'application' )
|
25
|
+
|
26
|
+
l = Leafy::Logger::Factory.get_logger 'my.app'
|
27
|
+
```
|
28
|
+
|
29
|
+
the configure will setup the (default) appenders and register the metrics.
|
30
|
+
|
31
|
+
there are more configurations for the logger Factory
|
32
|
+
```
|
33
|
+
require 'leafy/logger/appender_factories'
|
34
|
+
|
35
|
+
f.level = 'INFO'
|
36
|
+
f.loggers 'my.app' => 'WARN', 'my' => 'DEBUG'
|
37
|
+
f.appenders( Leafy::Logger::ConsoleAppenderFactory.new,
|
38
|
+
Leafy::Logger::FileAppenderFactory.new('log/my.log' ) )
|
39
|
+
f[ 'my.db' ] = 'INFO'
|
40
|
+
```
|
41
|
+
|
42
|
+
after the logger factory was configured once, any change of the log levels will result in a reconfiguration.
|
43
|
+
|
44
|
+
## appender factories
|
45
|
+
|
46
|
+
all appender factories have common set of attributes:
|
47
|
+
```
|
48
|
+
appender = Leafy::Logger::ConsoleAppenderFactory.new do
|
49
|
+
# :off, :trace, :debug, :info, :warn, :error, :all
|
50
|
+
self.threshold = :debug # default :all
|
51
|
+
self.log_format = "%-5p [%d{ISO8601,UTC}] %c: %m%n%ex"
|
52
|
+
self.queue_size = 1024 # default 256
|
53
|
+
# means nothing gets discarded
|
54
|
+
self.discarding_threshold = 0 # default -1
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
58
|
+
### console appender factory
|
59
|
+
|
60
|
+
```
|
61
|
+
appender = Leafy::Logger::ConsoleAppenderFactory.new do
|
62
|
+
self.target 'STDOUT' # default is 'STDERR'
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
66
|
+
### syslog appender factory
|
67
|
+
|
68
|
+
```
|
69
|
+
appender = Leafy::Logger::SyslogAppenderFactory.new do
|
70
|
+
self.host = '127.0.0.1' # default localhost
|
71
|
+
self.port = 514 # default 514
|
72
|
+
self.include_stack_trace = false # default true
|
73
|
+
# see Leafy::Logger::SyslogAppenderFactory.all_facilities
|
74
|
+
self.facility = :auth # default :local0
|
75
|
+
self.stack_trace_prefix = '__' # default '!'
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
### file appender factory
|
80
|
+
|
81
|
+
without archived files
|
82
|
+
|
83
|
+
```
|
84
|
+
appender = Leafy::Logger::SyslogAppenderFactory.new( 'logs/app.log' )
|
85
|
+
```
|
86
|
+
|
87
|
+
or
|
88
|
+
|
89
|
+
```
|
90
|
+
appender = Leafy::Logger::SyslogAppenderFactory.new do
|
91
|
+
self.current_log_filename = 'logs/app.log'
|
92
|
+
end
|
93
|
+
```
|
94
|
+
|
95
|
+
with archived files
|
96
|
+
|
97
|
+
```
|
98
|
+
appender = Leafy::Logger::SyslogAppenderFactory.new do
|
99
|
+
self.current_log_filename = 'logs/app.log'
|
100
|
+
self.archive = true # default true
|
101
|
+
# files ending with .gz or .zip will packed respectively
|
102
|
+
self.archived_log_filename_pattern = 'logs/app-%d.log'
|
103
|
+
self.archived_file_count = 10 # default 5
|
104
|
+
end
|
105
|
+
```
|
106
|
+
|
107
|
+
## developement
|
108
|
+
|
109
|
+
get all the gems and jars in place
|
110
|
+
|
111
|
+
gem install jar-dependencies --development
|
112
|
+
bundle install
|
113
|
+
|
114
|
+
please make sure you are using jar-dependencies >= 0.1.8 !
|
115
|
+
|
116
|
+
for running all specs
|
117
|
+
|
118
|
+
rake
|
119
|
+
|
120
|
+
or a single one
|
121
|
+
|
122
|
+
rspec spec/reporter_spec.rb
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#-*- mode: ruby -*-
|
2
|
+
|
3
|
+
require "rspec/core/rake_task"
|
4
|
+
RSpec::Core::RakeTask.new
|
5
|
+
|
6
|
+
require "yard"
|
7
|
+
YARD::Rake::YardocTask.new do |t|
|
8
|
+
t.files = ['lib/**/*.rb']
|
9
|
+
t.options += ["--title", "Leafy Rack Middleware"]
|
10
|
+
end
|
11
|
+
|
12
|
+
task :default => [ :spec ]
|
13
|
+
|
14
|
+
# vim: syntax=Ruby
|
@@ -0,0 +1,32 @@
|
|
1
|
+
#-*- mode: ruby -*-
|
2
|
+
|
3
|
+
require File.expand_path( '../lib/leafy/logger/version', __FILE__ )
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'leafy-logger'
|
7
|
+
s.version = Leafy::Logger::VERSION
|
8
|
+
s.author = 'christian meier'
|
9
|
+
s.email = [ 'christian.meier@lookout.com' ]
|
10
|
+
|
11
|
+
s.license = 'MIT'
|
12
|
+
s.summary = %q(adding logback to leafy)
|
13
|
+
s.homepage = 'https://github.com/lookout/leafy'
|
14
|
+
s.description = %q(adding logback to leafy with yaml configuration and bridges to log4j and jul)
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split($/)
|
17
|
+
|
18
|
+
# exclude the dependencies which come with jruby itself
|
19
|
+
# TODO needs something in jar-dependencies since those jruby dependencies can
|
20
|
+
# be different from version to version
|
21
|
+
|
22
|
+
s.requirements << 'jar io.dropwizard:dropwizard-logging, 0.8.0-rc5, [ joda-time:joda-time ]'
|
23
|
+
s.requirements << 'jar io.dropwizard:dropwizard-configuration, 0.8.0-rc5, [ org.yaml:snakeyaml ]'
|
24
|
+
|
25
|
+
s.add_runtime_dependency 'jar-dependencies', '~> 0.1.8'
|
26
|
+
s.add_runtime_dependency 'leafy-metrics', '~> 0.1.0'
|
27
|
+
s.add_development_dependency 'rspec', '~> 3.1.0'
|
28
|
+
s.add_development_dependency 'yard', '~> 0.8.7'
|
29
|
+
s.add_development_dependency 'rake', '~> 10.2'
|
30
|
+
end
|
31
|
+
|
32
|
+
# vim: syntax=Ruby
|
data/lib/leafy-logger.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'leafy-logger_jars'
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# this is a generated file, to avoid over-writing it just delete this comment
|
2
|
+
require 'jar_dependencies'
|
3
|
+
|
4
|
+
require_jar( 'org.slf4j', 'log4j-over-slf4j', '1.7.10' )
|
5
|
+
require_jar( 'com.google.guava', 'guava', '18.0' )
|
6
|
+
require_jar( 'org.slf4j', 'jul-to-slf4j', '1.7.10' )
|
7
|
+
require_jar( 'io.dropwizard', 'dropwizard-logging', '0.8.0-rc5' )
|
8
|
+
require_jar( 'org.slf4j', 'jcl-over-slf4j', '1.7.10' )
|
9
|
+
require_jar( 'org.jboss.logging', 'jboss-logging', '3.1.3.GA' )
|
10
|
+
require_jar( 'io.dropwizard', 'dropwizard-util', '0.8.0-rc5' )
|
11
|
+
require_jar( 'com.fasterxml.jackson.datatype', 'jackson-datatype-jdk7', '2.5.1' )
|
12
|
+
require_jar( 'org.apache.commons', 'commons-lang3', '3.3.2' )
|
13
|
+
require_jar( 'javax.validation', 'validation-api', '1.1.0.Final' )
|
14
|
+
require_jar( 'com.fasterxml.jackson.datatype', 'jackson-datatype-joda', '2.5.1' )
|
15
|
+
require_jar( 'com.fasterxml', 'classmate', '1.0.0' )
|
16
|
+
require_jar( 'org.hibernate', 'hibernate-validator', '5.1.3.Final' )
|
17
|
+
require_jar( 'org.eclipse.jetty', 'jetty-util', '9.2.9.v20150224' )
|
18
|
+
require_jar( 'io.dropwizard', 'dropwizard-configuration', '0.8.0-rc5' )
|
19
|
+
require_jar( 'com.fasterxml.jackson.datatype', 'jackson-datatype-guava', '2.5.1' )
|
20
|
+
require_jar( 'io.dropwizard', 'dropwizard-jackson', '0.8.0-rc5' )
|
21
|
+
require_jar( 'com.fasterxml.jackson.dataformat', 'jackson-dataformat-yaml', '2.5.1' )
|
22
|
+
require_jar( 'org.glassfish', 'javax.el', '3.0.0' )
|
23
|
+
require_jar( 'com.fasterxml.jackson.core', 'jackson-databind', '2.5.1' )
|
24
|
+
require_jar( 'ch.qos.logback', 'logback-classic', '1.1.2' )
|
25
|
+
require_jar( 'com.google.code.findbugs', 'jsr305', '3.0.0' )
|
26
|
+
require_jar( 'com.fasterxml.jackson.core', 'jackson-core', '2.5.1' )
|
27
|
+
require_jar( 'io.dropwizard.metrics', 'metrics-logback', '3.1.0' )
|
28
|
+
require_jar( 'io.dropwizard', 'dropwizard-validation', '0.8.0-rc5' )
|
29
|
+
require_jar( 'org.slf4j', 'slf4j-api', '1.7.10' )
|
30
|
+
require_jar( 'ch.qos.logback', 'logback-core', '1.1.2' )
|
31
|
+
require_jar( 'com.fasterxml.jackson.module', 'jackson-module-afterburner', '2.5.1' )
|
32
|
+
require_jar( 'com.fasterxml.jackson.core', 'jackson-annotations', '2.5.0' )
|
33
|
+
require_jar( 'io.dropwizard.metrics', 'metrics-core', '3.1.0' )
|
data/lib/leafy/logger.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'leafy-logger_jars'
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'leafy/logger'
|
2
|
+
module Leafy
|
3
|
+
module Logger
|
4
|
+
module AppenderFactory
|
5
|
+
|
6
|
+
def threshold
|
7
|
+
super.to_s
|
8
|
+
end
|
9
|
+
|
10
|
+
def threshold= t
|
11
|
+
super Java::ChQosLogbackClassic::Level.const_get( t.to_s.upcase )
|
12
|
+
end
|
13
|
+
|
14
|
+
def attributes
|
15
|
+
self.methods.select { |m| m.to_s =~ /^set_/ }.collect { |m| m.to_s.sub( /^set_/, '' ) }
|
16
|
+
end
|
17
|
+
|
18
|
+
module ClassMethods
|
19
|
+
def new( *args, &block )
|
20
|
+
if block
|
21
|
+
f = new( *args )
|
22
|
+
f.instance_eval( &block )
|
23
|
+
f
|
24
|
+
else
|
25
|
+
super
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class Java::IoDropwizardLogging::FileAppenderFactory
|
32
|
+
|
33
|
+
include AppenderFactory
|
34
|
+
extend AppenderFactory::ClassMethods
|
35
|
+
|
36
|
+
def initialize( file = nil )
|
37
|
+
super()
|
38
|
+
if file
|
39
|
+
self.current_log_filename = file
|
40
|
+
self.archive = false
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
FileAppenderFactory = Java::IoDropwizardLogging::FileAppenderFactory
|
45
|
+
|
46
|
+
class Java::IoDropwizardLogging::ConsoleAppenderFactory
|
47
|
+
|
48
|
+
include AppenderFactory
|
49
|
+
extend AppenderFactory::ClassMethods
|
50
|
+
|
51
|
+
alias :_target :target
|
52
|
+
def target( arg = nil )
|
53
|
+
if arg
|
54
|
+
self.target = arg
|
55
|
+
else
|
56
|
+
_target.to_s
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
alias :_target= :target=
|
61
|
+
def target= t
|
62
|
+
self._target = Java::IoDropwizardLogging::ConsoleAppenderFactory::ConsoleStream.value_of( t.to_s.upcase )
|
63
|
+
end
|
64
|
+
end
|
65
|
+
ConsoleAppenderFactory = Java::IoDropwizardLogging::ConsoleAppenderFactory
|
66
|
+
|
67
|
+
|
68
|
+
class Java::IoDropwizardLogging::SyslogAppenderFactory
|
69
|
+
|
70
|
+
include AppenderFactory
|
71
|
+
extend AppenderFactory::ClassMethods
|
72
|
+
|
73
|
+
def self.all_facilities
|
74
|
+
Java::IoDropwizardLogging::SyslogAppenderFactory::Facility.values.collect { |f| f.to_s }
|
75
|
+
end
|
76
|
+
|
77
|
+
alias :_facility :facility
|
78
|
+
def facility
|
79
|
+
_facility.to_s
|
80
|
+
end
|
81
|
+
|
82
|
+
alias :_facility= :facility=
|
83
|
+
def facility= t
|
84
|
+
self._facility = Java::IoDropwizardLogging::SyslogAppenderFactory::Facility.value_of( t.to_s.upcase )
|
85
|
+
end
|
86
|
+
end
|
87
|
+
SyslogAppenderFactory = Java::IoDropwizardLogging::SyslogAppenderFactory
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'leafy/logger'
|
2
|
+
require 'leafy/logger/appender_factories'
|
3
|
+
require 'yaml'
|
4
|
+
require 'stringio'
|
5
|
+
|
6
|
+
module Leafy
|
7
|
+
module Logger
|
8
|
+
class Factory
|
9
|
+
|
10
|
+
class HashSourceProvider
|
11
|
+
include Java::IoDropwizardConfiguration::ConfigurationSourceProvider
|
12
|
+
|
13
|
+
def initialize( config )
|
14
|
+
@config = config
|
15
|
+
end
|
16
|
+
|
17
|
+
def open(path)
|
18
|
+
StringIO.new( @config.to_yaml ).to_inputstream
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.get_logger( name )
|
23
|
+
org.slf4j.LoggerFactory.get_logger name
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.bootstrap
|
27
|
+
Java::IoDropwizardLogging::LoggingFactory.bootstrap
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.configurator
|
31
|
+
objectMapper = Java::IoDropwizardJackson::Jackson.newObjectMapper
|
32
|
+
validator = javax.validation.Validation.buildDefaultValidatorFactory.validator
|
33
|
+
Java::IoDropwizardConfiguration::ConfigurationFactory.new( Java::IoDropwizardLogging::LoggingFactory.java_class, validator, objectMapper, "" )
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.new_from( hash )
|
37
|
+
new( configurator.build( HashSourceProvider.new( hash ), 'dummy') )
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.new_from_yaml( yamlfile )
|
41
|
+
raise "no such file #{yamlfile}" unless File.exists?( yamlfile )
|
42
|
+
new( configurator.build( java.io.File.new( yamlfile ) ) )
|
43
|
+
end
|
44
|
+
|
45
|
+
def initialize( factory = nil )
|
46
|
+
@factory = factory || Java::IoDropwizardLogging::LoggingFactory.new
|
47
|
+
end
|
48
|
+
|
49
|
+
def level args = nil
|
50
|
+
if args
|
51
|
+
self.level = args
|
52
|
+
else
|
53
|
+
@factory.level.to_s
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def level= level
|
58
|
+
l = level.to_s.upcase
|
59
|
+
@factory.level = Java::ChQosLogbackClassic::Level.const_get( l )
|
60
|
+
reconfigure
|
61
|
+
end
|
62
|
+
|
63
|
+
def appenders( *args )
|
64
|
+
if args.size > 0
|
65
|
+
self.appenders = args
|
66
|
+
else
|
67
|
+
@factory.appenders.to_a
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def appenders= args
|
72
|
+
@factory.appenders = args
|
73
|
+
reconfigure
|
74
|
+
end
|
75
|
+
|
76
|
+
def loggers( map = nil )
|
77
|
+
if map
|
78
|
+
self.loggers = map
|
79
|
+
else
|
80
|
+
Hash[ @factory.loggers.collect {|k,v| [k, v.to_s ] } ]
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def []=( name, level )
|
85
|
+
m = @factory.loggers.to_hash
|
86
|
+
m[ name ] = level
|
87
|
+
self.loggers = m
|
88
|
+
end
|
89
|
+
|
90
|
+
def loggers= map
|
91
|
+
m = {}
|
92
|
+
map.each { |k,v| m[k] = Java::ChQosLogbackClassic::Level.const_get( v.to_s.upcase ) }
|
93
|
+
@factory.loggers = m
|
94
|
+
reconfigure
|
95
|
+
end
|
96
|
+
|
97
|
+
def configure( metrics, name )
|
98
|
+
@metrics = metrics
|
99
|
+
@name = name
|
100
|
+
reconfigure
|
101
|
+
end
|
102
|
+
|
103
|
+
def reconfigure
|
104
|
+
@factory.configure( @metrics.metrics, @name ) if @metrics
|
105
|
+
end
|
106
|
+
|
107
|
+
def stop
|
108
|
+
@factory.stop
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
@@ -0,0 +1,147 @@
|
|
1
|
+
require_relative 'setup'
|
2
|
+
require 'leafy/metrics/registry'
|
3
|
+
require 'leafy/logger/factory'
|
4
|
+
require 'leafy/logger/appender_factories'
|
5
|
+
require 'fileutils'
|
6
|
+
|
7
|
+
describe Leafy::Logger do
|
8
|
+
|
9
|
+
let( :factory ) do
|
10
|
+
f = Leafy::Logger::Factory.new
|
11
|
+
f.level :debug
|
12
|
+
f.configure( Leafy::Metrics::Registry.new, 'tester' )
|
13
|
+
f
|
14
|
+
end
|
15
|
+
|
16
|
+
let( :logger ) { Leafy::Logger::Factory.get_logger 'ROOT' }
|
17
|
+
|
18
|
+
describe Leafy::Logger::ConsoleAppenderFactory do
|
19
|
+
|
20
|
+
subject do
|
21
|
+
Leafy::Logger::ConsoleAppenderFactory.new do
|
22
|
+
target 'STDOUT'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'can set the target' do
|
27
|
+
expect(subject.target).to eq 'STDOUT'
|
28
|
+
|
29
|
+
subject.target = 'STDERR'
|
30
|
+
expect(subject.target).to eq 'STDERR'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe Leafy::Logger::SyslogAppenderFactory do
|
35
|
+
|
36
|
+
subject do
|
37
|
+
Leafy::Logger::SyslogAppenderFactory.new
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'can use default configuration with syslog' do
|
41
|
+
|
42
|
+
factory.appenders subject
|
43
|
+
|
44
|
+
logger.debug( 'debug' )
|
45
|
+
logger.info( 'info' )
|
46
|
+
|
47
|
+
factory.stop
|
48
|
+
|
49
|
+
if not system("netstat -uln |grep :514")
|
50
|
+
last = File.read( '/var/log/syslog' ).split( /\n/ ).last
|
51
|
+
expect(last).to match /info$/
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'can configure syslog' do
|
56
|
+
subject.port = 514
|
57
|
+
subject.include_stack_trace = false
|
58
|
+
subject.host = '127.0.0.1'
|
59
|
+
subject.facility = :auth
|
60
|
+
subject.stack_trace_prefix = '__'
|
61
|
+
subject.threshold = :debug
|
62
|
+
|
63
|
+
factory.appenders subject
|
64
|
+
|
65
|
+
logger.debug( 'debug' )
|
66
|
+
logger.info( 'info', java.lang.Exception.new( 'help me' ) )
|
67
|
+
|
68
|
+
factory.stop
|
69
|
+
|
70
|
+
if not system("netstat -uln |grep :514") and File.exist?( '/var/log/auth.log' )
|
71
|
+
last = File.read( '/var/log/auth.log' ).split( /\n/ ).last
|
72
|
+
expect(last).to match /info$/
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe Leafy::Logger::FileAppenderFactory do
|
78
|
+
|
79
|
+
subject do
|
80
|
+
Leafy::Logger::FileAppenderFactory.new
|
81
|
+
end
|
82
|
+
|
83
|
+
let( :log ) { File.join( File.dirname( __FILE__ ), 'test.log' ) }
|
84
|
+
|
85
|
+
before { FileUtils.rm_f log }
|
86
|
+
after { FileUtils.rm_f log }
|
87
|
+
|
88
|
+
it 'can be configured with single file' do
|
89
|
+
subject.current_log_filename = log
|
90
|
+
subject.archive = false
|
91
|
+
subject.threshold = 'INFO'
|
92
|
+
subject.log_format = "%-5p [%d{ISO8601,UTC}] %c: %m%n%ex"
|
93
|
+
|
94
|
+
factory.appenders subject
|
95
|
+
logger.debug( 'debug' )
|
96
|
+
logger.info( 'info', java.lang.Exception.new( 'help' ) )
|
97
|
+
|
98
|
+
factory.stop
|
99
|
+
|
100
|
+
lines = File.read( log ).split( /\n/ )
|
101
|
+
lines.each do |line|
|
102
|
+
expect( line ).to match /^INFO.*info|^!.*help|^!.*at/
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'can be configured via block' do
|
107
|
+
l = log
|
108
|
+
s = subject.class.new do
|
109
|
+
self.current_log_filename = l
|
110
|
+
self.archive = false
|
111
|
+
self.threshold = 'DEBUG'
|
112
|
+
self.log_format = "%-5p [%d{ISO8601,UTC}] %c: %m%n%rEx"
|
113
|
+
end
|
114
|
+
|
115
|
+
factory.appenders s
|
116
|
+
logger.debug( 'debug' )
|
117
|
+
logger.info( 'info', java.lang.Exception.new( 'help' ) )
|
118
|
+
|
119
|
+
factory.stop
|
120
|
+
|
121
|
+
lines = File.read( log ).split( /\n/ )
|
122
|
+
lines.each do |line|
|
123
|
+
expect( line ).to match /^INFO.*info|^DEBUG.*debug|^!.*help|^!.*at/
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
it 'can be configured with archived files' do
|
128
|
+
subject.current_log_filename = log
|
129
|
+
subject.archived_log_filename_pattern = "#{log}-%d.gz"
|
130
|
+
subject.archived_file_count = 2
|
131
|
+
subject.threshold = 'DEBUG'
|
132
|
+
subject.log_format = "%-5p [%d{ISO8601,UTC}] %c: %m%n%rEx"
|
133
|
+
|
134
|
+
factory.appenders subject
|
135
|
+
|
136
|
+
logger.debug( 'debug' )
|
137
|
+
logger.info( 'info', java.lang.Exception.new( 'help' ) )
|
138
|
+
|
139
|
+
factory.stop
|
140
|
+
|
141
|
+
lines = File.read( log ).split( /\n/ )
|
142
|
+
lines.each do |line|
|
143
|
+
expect( line ).to match /^INFO.*info|^DEBUG.*debug|^!.*help|^!.*at/
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
@@ -0,0 +1,155 @@
|
|
1
|
+
require_relative 'setup'
|
2
|
+
require 'leafy/metrics/registry'
|
3
|
+
require 'leafy/logger/factory'
|
4
|
+
require 'fileutils'
|
5
|
+
|
6
|
+
describe Leafy::Logger::Factory do
|
7
|
+
|
8
|
+
subject do
|
9
|
+
f = Leafy::Logger::Factory.new
|
10
|
+
f.appenders( Leafy::Logger::FileAppenderFactory.new( log ) )
|
11
|
+
f.configure( Leafy::Metrics::Registry.new, 'tester' )
|
12
|
+
f
|
13
|
+
end
|
14
|
+
|
15
|
+
let( :log ) { File.join( File.dirname( __FILE__ ), 'test.log' ) }
|
16
|
+
let( :yaml ) { File.join( File.dirname( __FILE__ ), 'logging.yml' ) }
|
17
|
+
let( :logger1 ) { Leafy::Logger::Factory.get_logger 'my.app' }
|
18
|
+
let( :logger2 ) { Leafy::Logger::Factory.get_logger 'my.db' }
|
19
|
+
|
20
|
+
before { FileUtils.rm_f log }
|
21
|
+
after { FileUtils.rm_f log }
|
22
|
+
|
23
|
+
it 'can set appenders' do
|
24
|
+
expect( subject.appenders.size ).to eq 1
|
25
|
+
|
26
|
+
subject.appenders Leafy::Logger::ConsoleAppenderFactory.new
|
27
|
+
expect( subject.appenders.size ).to eq 1
|
28
|
+
|
29
|
+
subject.appenders( Leafy::Logger::ConsoleAppenderFactory.new,
|
30
|
+
Leafy::Logger::FileAppenderFactory.new( log ) )
|
31
|
+
expect( subject.appenders.size ).to eq 2
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'sets the default level' do
|
35
|
+
subject.level = 'INFO'
|
36
|
+
expect( subject.level ).to eq 'INFO'
|
37
|
+
|
38
|
+
subject.level 'DEBUG'
|
39
|
+
expect( subject.level ).to eq 'DEBUG'
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'can set the default log level' do
|
43
|
+
subject.level = 'INFO'
|
44
|
+
|
45
|
+
logger1.debug 'debug1'
|
46
|
+
logger1.info 'good'
|
47
|
+
logger1.warn 'good'
|
48
|
+
logger2.debug 'debug2'
|
49
|
+
logger2.info 'good'
|
50
|
+
logger2.warn 'good'
|
51
|
+
|
52
|
+
subject.stop
|
53
|
+
|
54
|
+
lines = File.read( log ).split( /\n/ )
|
55
|
+
|
56
|
+
expect( lines.size ).to eq 4
|
57
|
+
lines.each do |line|
|
58
|
+
expect( line ).to match /INFO|WARN/
|
59
|
+
expect( line ).to match /good/
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'can set a specific log level' do
|
64
|
+
expect( subject.loggers ).to eq Hash[]
|
65
|
+
|
66
|
+
subject[ 'my.app' ] = 'WARN'
|
67
|
+
|
68
|
+
expect( subject.loggers ).to eq Hash[ 'my.app' => 'WARN' ]
|
69
|
+
|
70
|
+
logger1.debug 'debug1'
|
71
|
+
logger1.info 'info1'
|
72
|
+
logger1.warn 'good'
|
73
|
+
logger2.debug 'debug2'
|
74
|
+
logger2.info 'good'
|
75
|
+
logger2.warn 'good'
|
76
|
+
|
77
|
+
subject.stop
|
78
|
+
|
79
|
+
lines = File.read( log ).split( /\n/ )
|
80
|
+
|
81
|
+
expect( lines.size ).to eq 3
|
82
|
+
lines.each do |line|
|
83
|
+
expect( line ).to match /INFO|WARN/
|
84
|
+
expect( line ).to match /good/
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'can set a specific log level' do
|
89
|
+
expect( subject.loggers ).to eq Hash[]
|
90
|
+
|
91
|
+
subject.loggers( 'my' => 'WARN' )
|
92
|
+
|
93
|
+
expect( subject.loggers ).to eq Hash[ 'my' => 'WARN' ]
|
94
|
+
|
95
|
+
logger1.debug 'debug1'
|
96
|
+
logger1.info 'info1'
|
97
|
+
logger1.warn 'good'
|
98
|
+
logger2.debug 'debug2'
|
99
|
+
logger2.info 'info2'
|
100
|
+
logger2.warn 'good'
|
101
|
+
|
102
|
+
subject.stop
|
103
|
+
|
104
|
+
lines = File.read( log ).split( /\n/ )
|
105
|
+
|
106
|
+
expect( lines.size ).to eq 2
|
107
|
+
lines.each do |line|
|
108
|
+
expect( line ).to match /WARN/
|
109
|
+
expect( line ).to match /good/
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'fails on missing yaml configuration' do
|
115
|
+
expect {Leafy::Logger::Factory.new_from_yaml( yaml + '.gone' ) }.to raise_error( Exception )
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'can use a yaml configuration' do
|
119
|
+
f = Leafy::Logger::Factory.new_from_yaml( yaml )
|
120
|
+
expect( f.level ).to eq 'ERROR'
|
121
|
+
expect( f.loggers ).to eq 'com.example.app' => 'DEBUG'
|
122
|
+
expect( f.appenders.size ).to eq 3
|
123
|
+
# console
|
124
|
+
expect( f.appenders[0].threshold ).to eq 'DEBUG'
|
125
|
+
expect( f.appenders[0].target ).to eq 'STDERR'
|
126
|
+
# file
|
127
|
+
expect( f.appenders[1].threshold ).to eq 'INFO'
|
128
|
+
expect( f.appenders[1].current_log_filename ).to eq './logs/example.log'
|
129
|
+
expect( f.appenders[1].archived_log_filename_pattern ).to eq './logs/example-%d.log.gz'
|
130
|
+
expect( f.appenders[1].archived_file_count ).to eq 12
|
131
|
+
# syslog
|
132
|
+
expect( f.appenders[2].host ).to eq 'myhost'
|
133
|
+
expect( f.appenders[2].port ).to eq 123
|
134
|
+
expect( f.appenders[2].facility ).to eq 'KERN'
|
135
|
+
end
|
136
|
+
|
137
|
+
it 'can use a hash configuration' do
|
138
|
+
f = Leafy::Logger::Factory.new_from( YAML.load( File.read( yaml ) ) )
|
139
|
+
expect( f.level ).to eq 'ERROR'
|
140
|
+
expect( f.loggers ).to eq 'com.example.app' => 'DEBUG'
|
141
|
+
expect( f.appenders.size ).to eq 3
|
142
|
+
# console
|
143
|
+
expect( f.appenders[0].threshold ).to eq 'DEBUG'
|
144
|
+
expect( f.appenders[0].target ).to eq 'STDERR'
|
145
|
+
# file
|
146
|
+
expect( f.appenders[1].threshold ).to eq 'INFO'
|
147
|
+
expect( f.appenders[1].current_log_filename ).to eq './logs/example.log'
|
148
|
+
expect( f.appenders[1].archived_log_filename_pattern ).to eq './logs/example-%d.log.gz'
|
149
|
+
expect( f.appenders[1].archived_file_count ).to eq 12
|
150
|
+
# syslog
|
151
|
+
expect( f.appenders[2].host ).to eq 'myhost'
|
152
|
+
expect( f.appenders[2].port ).to eq 123
|
153
|
+
expect( f.appenders[2].facility ).to eq 'KERN'
|
154
|
+
end
|
155
|
+
end
|
data/spec/logging.yml
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
level: ERROR
|
2
|
+
loggers:
|
3
|
+
com.example.app: DEBUG
|
4
|
+
appenders:
|
5
|
+
- type: console
|
6
|
+
threshold: DEBUG
|
7
|
+
target: STDERR
|
8
|
+
- type: file
|
9
|
+
threshold: INFO
|
10
|
+
currentLogFilename: ./logs/example.log
|
11
|
+
archivedLogFilenamePattern: ./logs/example-%d.log.gz
|
12
|
+
archivedFileCount: 12
|
13
|
+
- type: syslog
|
14
|
+
host: myhost
|
15
|
+
port: 123
|
16
|
+
facility: KERN
|
data/spec/setup.rb
ADDED
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: leafy-logger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- christian meier
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - ~>
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: 0.1.8
|
19
|
+
name: jar-dependencies
|
20
|
+
prerelease: false
|
21
|
+
type: :runtime
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.1.8
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.1.0
|
33
|
+
name: leafy-metrics
|
34
|
+
prerelease: false
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.1.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ~>
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 3.1.0
|
47
|
+
name: rspec
|
48
|
+
prerelease: false
|
49
|
+
type: :development
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.1.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ~>
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 0.8.7
|
61
|
+
name: yard
|
62
|
+
prerelease: false
|
63
|
+
type: :development
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.8.7
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ~>
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '10.2'
|
75
|
+
name: rake
|
76
|
+
prerelease: false
|
77
|
+
type: :development
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.2'
|
83
|
+
description: adding logback to leafy with yaml configuration and bridges to log4j and jul
|
84
|
+
email:
|
85
|
+
- christian.meier@lookout.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- .gitignore
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- leafy-logger.gemspec
|
96
|
+
- lib/leafy-logger.rb
|
97
|
+
- lib/leafy-logger_jars.rb
|
98
|
+
- lib/leafy/logger.rb
|
99
|
+
- lib/leafy/logger/appender_factories.rb
|
100
|
+
- lib/leafy/logger/factory.rb
|
101
|
+
- lib/leafy/logger/version.rb
|
102
|
+
- spec/appender_factories_spec.rb
|
103
|
+
- spec/factory_spec.rb
|
104
|
+
- spec/logging.yml
|
105
|
+
- spec/setup.rb
|
106
|
+
homepage: https://github.com/lookout/leafy
|
107
|
+
licenses:
|
108
|
+
- MIT
|
109
|
+
metadata: {}
|
110
|
+
post_install_message:
|
111
|
+
rdoc_options: []
|
112
|
+
require_paths:
|
113
|
+
- lib
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - '>='
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - '>='
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
requirements:
|
125
|
+
- jar io.dropwizard:dropwizard-logging, 0.8.0-rc5, [ joda-time:joda-time ]
|
126
|
+
- jar io.dropwizard:dropwizard-configuration, 0.8.0-rc5, [ org.yaml:snakeyaml ]
|
127
|
+
rubyforge_project:
|
128
|
+
rubygems_version: 2.4.5
|
129
|
+
signing_key:
|
130
|
+
specification_version: 4
|
131
|
+
summary: adding logback to leafy
|
132
|
+
test_files: []
|
133
|
+
has_rdoc:
|