leafy-logger 0.4.0-java

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 107b48477ed48b181e933cbf7c47a464069bdc1c
4
+ data.tar.gz: 21a8dfd6bdc5accf70202f7cda9951d4b440b7d8
5
+ SHA512:
6
+ metadata.gz: c88c8f6244c2694ed4a46a07365d0d3b017c49d04b3519ec55519b65779b4880a4554c37eb3c72dfd51a32f86523c23e86644e4bb20b9ce0f9b629b5cc5525d6
7
+ data.tar.gz: d4e4899521d6da93efd8b1480b45256c6bbe7d4d92a83e5af9f077509a0983d28be632ee420e11b6b8ba45dee9c4886a5c54103631086c1de494a387b4b2ac3c
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ .yardoc
2
+ doc
3
+ pkg
4
+ *.lock
5
+ logs
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ #-*- mode: ruby -*-
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
6
+
7
+ gem 'leafy-metrics', :path => '../leafy-metrics'
8
+
9
+ # vim: syntax=Ruby
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,174 @@
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
+ # using configurations
108
+
109
+ a yaml file or an options hash can be used to configure the logger factory. once you have a logger factory you can reconfigure it with a yaml-file or options hash at runtime.
110
+
111
+ ## build logger factory from yaml file
112
+
113
+ having a yaml file like (config/logging.yaml)
114
+
115
+ ```
116
+ level: ERROR
117
+ loggers:
118
+ com.example.app: DEBUG
119
+ com.example.db: INFO
120
+ appenders:
121
+ - type: console
122
+ threshold: DEBUG
123
+ target: STDERR
124
+ - type: file
125
+ threshold: INFO
126
+ currentLogFilename: ./logs/example.log
127
+ archivedLogFilenamePattern: ./logs/example-%d.log.gz
128
+ archivedFileCount: 12
129
+ - type: syslog
130
+ host: 127.0.0.1
131
+ port: 123
132
+ facility: KERN
133
+ ```
134
+ can be used to build ```LoggerFactory``` directly from this yaml file:
135
+
136
+ ```
137
+ factory = Leafy::Logger::Factory.new_from_yaml( 'config/logging.yaml' )
138
+ ```
139
+
140
+ having the same config as ```Hash``` can be used as well
141
+
142
+ ```
143
+ options = YAML.load( File.read( 'config/logging.yaml' ) )
144
+ factory = Leafy::Logger::Factory.new_from_options( options )
145
+ ```
146
+
147
+ ## reconfigure logger factory at runtime
148
+
149
+ ```
150
+ factory.reconfigure_from_yaml( 'config/logging.yaml' )
151
+ ```
152
+
153
+ or
154
+
155
+ ```
156
+ factory.reconfigure_from_options( options )
157
+ ```
158
+
159
+ ## developement
160
+
161
+ get all the gems and jars in place
162
+
163
+ gem install jar-dependencies --development
164
+ bundle install
165
+
166
+ please make sure you are using jar-dependencies >= 0.1.8 !
167
+
168
+ for running all specs
169
+
170
+ rake
171
+
172
+ or a single one
173
+
174
+ 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,33 @@
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.platform = 'java'
12
+ s.license = 'MIT'
13
+ s.summary = %q(adding logback to leafy)
14
+ s.homepage = 'https://github.com/lookout/leafy'
15
+ s.description = %q(adding logback to leafy with yaml configuration and bridges to log4j and jul)
16
+
17
+ s.files = `git ls-files`.split($/)
18
+
19
+ # exclude the dependencies which come with jruby itself
20
+ # TODO needs something in jar-dependencies since those jruby dependencies can
21
+ # be different from version to version
22
+
23
+ s.requirements << 'jar io.dropwizard:dropwizard-logging, 0.8.0-rc5, [ joda-time:joda-time ]'
24
+ s.requirements << 'jar io.dropwizard:dropwizard-configuration, 0.8.0-rc5, [ org.yaml:snakeyaml ]'
25
+
26
+ s.add_runtime_dependency 'jar-dependencies', '~> 0.1.8'
27
+ s.add_runtime_dependency 'leafy-metrics', "~> #{Leafy::Logger::VERSION}"
28
+ s.add_development_dependency 'rspec', '~> 3.1'
29
+ s.add_development_dependency 'yard', '~> 0.8.7'
30
+ s.add_development_dependency 'rake', '~> 10.2'
31
+ end
32
+
33
+ # vim: syntax=Ruby
@@ -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,141 @@
1
+ require 'leafy/logger'
2
+ require 'leafy/logger/appender_factories'
3
+ require 'yaml'
4
+ require 'stringio'
5
+ require 'jruby/synchronized'
6
+
7
+ module Leafy
8
+ module Logger
9
+ class Factory
10
+ include JRuby::Synchronized
11
+
12
+ class HashSourceProvider
13
+ include Java::IoDropwizardConfiguration::ConfigurationSourceProvider
14
+
15
+ def initialize( config )
16
+ @config = config
17
+ end
18
+
19
+ def open(path)
20
+ StringIO.new( @config.to_yaml ).to_inputstream
21
+ end
22
+ end
23
+
24
+ def self.get_logger( name )
25
+ org.slf4j.LoggerFactory.get_logger name
26
+ end
27
+
28
+ def self.bootstrap
29
+ Java::IoDropwizardLogging::LoggingFactory.bootstrap
30
+ end
31
+
32
+ def self.configurator
33
+ objectMapper = Java::IoDropwizardJackson::Jackson.newObjectMapper
34
+ validator = javax.validation.Validation.buildDefaultValidatorFactory.validator
35
+ Java::IoDropwizardConfiguration::ConfigurationFactory.new( Java::IoDropwizardLogging::LoggingFactory.java_class, validator, objectMapper, "" )
36
+ end
37
+
38
+ def self.new_from_options( options )
39
+ new( configurator.build( HashSourceProvider.new( options ), 'dummy') )
40
+ end
41
+
42
+ def self.new_from_yaml( yamlfile )
43
+ raise "no such file #{yamlfile}" unless File.exists?( yamlfile )
44
+ new( configurator.build( java.io.File.new( yamlfile ) ) )
45
+ end
46
+
47
+ def initialize( factory = nil )
48
+ @factory = factory || Java::IoDropwizardLogging::LoggingFactory.new
49
+ end
50
+
51
+ def reconfigure_from_options( options )
52
+ do_reconfigure( self.class.configurator.build( HashSourceProvider.new( options ), 'dummy') )
53
+ end
54
+
55
+ def reconfigure_from_yaml( yamlfile )
56
+ raise "no such file #{yamlfile}" unless File.exists?( yamlfile )
57
+ do_reconfigure( self.class.configurator.build( java.io.File.new( yamlfile ) ) )
58
+ end
59
+
60
+ def do_reconfigure( factory )
61
+ @factory.stop
62
+ @factory = factory
63
+ reconfigure
64
+ end
65
+ private :do_reconfigure
66
+
67
+ def level args = nil
68
+ if args
69
+ self.level = args
70
+ else
71
+ @factory.level.to_s
72
+ end
73
+ end
74
+
75
+ def level_set level
76
+ l = level.to_s.upcase
77
+ @factory.level = Java::ChQosLogbackClassic::Level.const_get( l )
78
+ end
79
+ private :level_set
80
+
81
+ def level= level
82
+ l = level.to_s.upcase
83
+ @factory.level = Java::ChQosLogbackClassic::Level.const_get( l )
84
+ reconfigure
85
+ end
86
+
87
+ def appenders( *args )
88
+ if args.size > 0
89
+ self.appenders = args
90
+ else
91
+ @factory.appenders.to_a
92
+ end
93
+ end
94
+
95
+ def appenders= args
96
+ @factory.appenders = args
97
+ reconfigure
98
+ end
99
+
100
+ def loggers( map = nil )
101
+ if map
102
+ self.loggers = map
103
+ else
104
+ Hash[ @factory.loggers.collect {|k,v| [k, v.to_s ] } ]
105
+ end
106
+ end
107
+
108
+ def []=( name, level )
109
+ m = @factory.loggers.to_hash
110
+ m[ name ] = level
111
+ self.loggers = m
112
+ end
113
+
114
+ def loggers_set map
115
+ m = {}
116
+ map.each { |k,v| m[k] = Java::ChQosLogbackClassic::Level.const_get( v.to_s.upcase ) }
117
+ @factory.loggers = m
118
+ end
119
+ private :loggers_set
120
+
121
+ def loggers= map
122
+ loggers_set( map )
123
+ reconfigure
124
+ end
125
+
126
+ def configure( metrics, name )
127
+ @metrics = metrics
128
+ @name = name
129
+ reconfigure
130
+ end
131
+
132
+ def reconfigure
133
+ @factory.configure( @metrics.metrics, @name ) if @metrics
134
+ end
135
+
136
+ def stop
137
+ @factory.stop
138
+ end
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,6 @@
1
+ module Leafy
2
+ module Logger
3
+ VERSION = '0.4.0'.freeze
4
+ end
5
+ end
6
+
@@ -0,0 +1 @@
1
+ require 'leafy-logger_jars'
@@ -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' )
@@ -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,176 @@
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
+ let( :factory_yaml ) { Leafy::Logger::Factory.new_from_yaml( yaml ) }
21
+ let( :reconfigured_yaml ) do
22
+ subject.reconfigure_from_yaml( yaml )
23
+ subject
24
+ end
25
+
26
+ let( :options ) { YAML.load( File.read( yaml ) ) }
27
+ let( :factory_options ) { Leafy::Logger::Factory.new_from_options( options ) }
28
+ let( :reconfigured_options ) do
29
+ subject.reconfigure_from_options( options )
30
+ subject
31
+ end
32
+
33
+ before { FileUtils.rm_f log }
34
+ after { FileUtils.rm_f log }
35
+
36
+ it 'can set appenders' do
37
+ expect( subject.appenders.size ).to eq 1
38
+
39
+ subject.appenders Leafy::Logger::ConsoleAppenderFactory.new
40
+ expect( subject.appenders.size ).to eq 1
41
+
42
+ subject.appenders( Leafy::Logger::ConsoleAppenderFactory.new,
43
+ Leafy::Logger::FileAppenderFactory.new( log ) )
44
+ expect( subject.appenders.size ).to eq 2
45
+ end
46
+
47
+ it 'sets the default level' do
48
+ subject.level = 'INFO'
49
+ expect( subject.level ).to eq 'INFO'
50
+
51
+ subject.level 'DEBUG'
52
+ expect( subject.level ).to eq 'DEBUG'
53
+ end
54
+
55
+ it 'can set the default log level' do
56
+ subject.level = 'INFO'
57
+
58
+ logger1.debug 'debug1'
59
+ logger1.info 'good'
60
+ logger1.warn 'good'
61
+ logger2.debug 'debug2'
62
+ logger2.info 'good'
63
+ logger2.warn 'good'
64
+
65
+ subject.stop
66
+
67
+ lines = File.read( log ).split( /\n/ )
68
+
69
+ expect( lines.size ).to eq 4
70
+ lines.each do |line|
71
+ expect( line ).to match /INFO|WARN/
72
+ expect( line ).to match /good/
73
+ end
74
+ end
75
+
76
+ it 'can set a specific log level' do
77
+ expect( subject.loggers ).to eq Hash[]
78
+
79
+ subject[ 'my.app' ] = 'WARN'
80
+
81
+ expect( subject.loggers ).to eq Hash[ 'my.app' => 'WARN' ]
82
+
83
+ logger1.debug 'debug1'
84
+ logger1.info 'info1'
85
+ logger1.warn 'good'
86
+ logger2.debug 'debug2'
87
+ logger2.info 'good'
88
+ logger2.warn 'good'
89
+
90
+ subject.stop
91
+
92
+ lines = File.read( log ).split( /\n/ )
93
+
94
+ expect( lines.size ).to eq 3
95
+ lines.each do |line|
96
+ expect( line ).to match /INFO|WARN/
97
+ expect( line ).to match /good/
98
+ end
99
+ end
100
+
101
+ it 'can set a specific log level' do
102
+ expect( subject.loggers ).to eq Hash[]
103
+
104
+ subject.loggers( 'my' => 'WARN' )
105
+
106
+ expect( subject.loggers ).to eq Hash[ 'my' => 'WARN' ]
107
+
108
+ logger1.debug 'debug1'
109
+ logger1.info 'info1'
110
+ logger1.warn 'good'
111
+ logger2.debug 'debug2'
112
+ logger2.info 'info2'
113
+ logger2.warn 'good'
114
+
115
+ subject.stop
116
+
117
+ lines = File.read( log ).split( /\n/ )
118
+
119
+ expect( lines.size ).to eq 2
120
+ lines.each do |line|
121
+ expect( line ).to match /WARN/
122
+ expect( line ).to match /good/
123
+ end
124
+
125
+ end
126
+
127
+ it 'fails on missing yaml configuration' do
128
+ expect {Leafy::Logger::Factory.new_from_yaml( yaml + '.gone' ) }.to raise_error( Exception )
129
+ end
130
+
131
+ it 'fails reconfigure on missing yaml configuration' do
132
+ expect {subject.reconfigure_from_yaml( yaml + '.gone' ) }.to raise_error( Exception )
133
+ end
134
+
135
+ [ :factory_yaml, :reconfigured_yaml ].each do |method|
136
+ it "can use a yaml configuration - #{method}" do
137
+ f = send( method )
138
+ expect( f.level ).to eq 'ERROR'
139
+ expect( f.loggers ).to eq 'com.example.app' => 'DEBUG'
140
+ expect( f.appenders.size ).to eq 3
141
+ # console
142
+ expect( f.appenders[0].threshold ).to eq 'DEBUG'
143
+ expect( f.appenders[0].target ).to eq 'STDERR'
144
+ # file
145
+ expect( f.appenders[1].threshold ).to eq 'INFO'
146
+ expect( f.appenders[1].current_log_filename ).to eq './logs/example.log'
147
+ expect( f.appenders[1].archived_log_filename_pattern ).to eq './logs/example-%d.log.gz'
148
+ expect( f.appenders[1].archived_file_count ).to eq 12
149
+ # syslog
150
+ expect( f.appenders[2].host ).to eq '127.0.0.1'
151
+ expect( f.appenders[2].port ).to eq 123
152
+ expect( f.appenders[2].facility ).to eq 'KERN'
153
+ end
154
+ end
155
+
156
+ [ :factory_options, :reconfigured_options ].each do |method|
157
+ it 'can use a hash configuration' do
158
+ f = send( method )
159
+ expect( f.level ).to eq 'ERROR'
160
+ expect( f.loggers ).to eq 'com.example.app' => 'DEBUG'
161
+ expect( f.appenders.size ).to eq 3
162
+ # console
163
+ expect( f.appenders[0].threshold ).to eq 'DEBUG'
164
+ expect( f.appenders[0].target ).to eq 'STDERR'
165
+ # file
166
+ expect( f.appenders[1].threshold ).to eq 'INFO'
167
+ expect( f.appenders[1].current_log_filename ).to eq './logs/example.log'
168
+ expect( f.appenders[1].archived_log_filename_pattern ).to eq './logs/example-%d.log.gz'
169
+ expect( f.appenders[1].archived_file_count ).to eq 12
170
+ # syslog
171
+ expect( f.appenders[2].host ).to eq '127.0.0.1'
172
+ expect( f.appenders[2].port ).to eq 123
173
+ expect( f.appenders[2].facility ).to eq 'KERN'
174
+ end
175
+ end
176
+ 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: 127.0.0.1
15
+ port: 123
16
+ facility: KERN
data/spec/setup.rb ADDED
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path( '../../lib', __FILE__ )
2
+ require 'jar-dependencies'
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: leafy-logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: java
6
+ authors:
7
+ - christian meier
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-21 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.4.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.4.0
41
+ - !ruby/object:Gem::Dependency
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: '3.1'
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'
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: