mojo_logger 0.1.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dc5077befcd1a3215bfc2bc080e94e1ab3810479
4
- data.tar.gz: f63f06a080617ba976d3b8270b3809d6ed58bf9f
3
+ metadata.gz: b542540d5e724fed35f873cc7e639b35fc27ed60
4
+ data.tar.gz: e9559ef49e62808e55adb5e8da62f3c7c1a517aa
5
5
  SHA512:
6
- metadata.gz: 14b21d9122cebc7b95d94a577c4dc761ade4abfc6ab78b54b357a51f854ede05afb05ee31fa71872aa92ef81e471673b8fae61e81035bf0d43bf66e492019087
7
- data.tar.gz: e84d470a60e7d395b2e9b938957ca80a6cffbe4b56392075f141de37a21b399d150b50204e99c8c41e83d139e4ef290bb58a34183298528ee7665fbb28c4267d
6
+ metadata.gz: ac210ac71c04b8590d8f94e42349f636244a6ee3ad0eee8b9879e1a30d899fff6ab50cc019a673764ca409e73eeaea1b2171fb9c6795af04468d078b9681402e
7
+ data.tar.gz: 80ce95be39775bf75c9383866ced7d64349987f3c3010d19864c7afa7adcf07cf701e89bd9d2d219b833f6b2d2be5f7adf0d0e51ec547c904c6afc441405a010
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ jruby-9.1.2.0
data/Gemfile CHANGED
@@ -2,7 +2,3 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in mojo_logger.gemspec
4
4
  gemspec
5
-
6
- group :test do
7
- gem 'rspec'
8
- end
data/README.md CHANGED
@@ -1,8 +1,9 @@
1
1
  # MojoLogger
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/mojo_logger`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ This gem is a wrapper around log4j and is primarily meant to be used in the Mojo framework. It will standardize logs accross Mojo applications and provide a single location to edit the Mojo Logger code base. It comes with a few benefits:
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ * **JSON** Logs will be printed for nice splunk logging when the mojo_* methods are called.
6
+ * No need for a log4j.properties file (although you can use one if you prefer). You can configure the logger on the fly and this gem will build a StringIO object to mock the log4j.properties file
6
7
 
7
8
  ## Installation
8
9
 
@@ -22,7 +23,129 @@ Or install it yourself as:
22
23
 
23
24
  ## Usage
24
25
 
25
- TODO: Write usage instructions here
26
+ #### Simple access to a CONSOLE appender:
27
+
28
+ ```ruby
29
+ require 'mojo_logger'
30
+
31
+ MojoLogger.debug "This is a message"
32
+ #=> This is a message
33
+
34
+ ```
35
+
36
+ #### Mojo Formatted JSON logs
37
+ ```ruby
38
+ require 'mojo_logger'
39
+
40
+ api_request = {
41
+ session_id: '111111-11111',
42
+ reference_id: 'client',
43
+ api: 'get-user-info'
44
+ }
45
+
46
+ MojoLogger.mojo_warn(api_request, "Oops", "Some Warn Message")
47
+ #=> {"time":"08-25-2015 19:22:59.940 +0000","app":"Mojo","session_id":"111111-11111","reference_id":"client","api":"get-user-info","category":"Oops","message":"Some Warn Message"}
48
+
49
+ ```
50
+
51
+ #### Mojo Formatted JSON logs with cusom keys
52
+ ```ruby
53
+ require 'mojo_logger'
54
+
55
+ api_request = {
56
+ session_id: '111111-11111',
57
+ reference_id: 'client',
58
+ api: 'get-user-info'
59
+ }
60
+
61
+ MojoLogger.mojo_warn(api_request, "Oops", "Some Warn Message", {my_key: "Some value", my_other_key: :another_val})
62
+ #=> {"time":"08-25-2015 19:24:57.575 +0000","app":"Mojo","session_id":"111111-11111","reference_id":"client","api":"get-user-info","category":"Oops","message":"Some Warn Message","my_key":"Some value","my_other_key":"another_val"}
63
+
64
+ ```
65
+
66
+
67
+ ## Configuration
68
+
69
+ #### CONSOLE appender with "WARN" log level
70
+ ```ruby
71
+ require 'mojo_logger'
72
+
73
+ MojoLogger.config do |config|
74
+ config.default_log_level = :warn
75
+ end
76
+
77
+ MojoLogger.debug "This won't show up"
78
+ #=> nil
79
+
80
+ MojoLogger.warn "This will show up"
81
+ #=> This will show up
82
+
83
+ ```
84
+
85
+
86
+ #### Rolling File appender
87
+ This will create a rolling file logger rather than a console logger with the following settings:
88
+ Max File Size = 10MB
89
+ Max Backup Index = 10
90
+ Pattern = '%n %m %n' (I need to change this but this is what it is for now.)
91
+
92
+ ```ruby
93
+ require 'mojo_logger'
94
+
95
+ MojoLogger.config do |config|
96
+ config.log_file = '/var/log/my_log.log'
97
+ end
98
+
99
+ ```
100
+
101
+ #### Add Custom lines to the generated log4j.properties 'file'
102
+
103
+ ```ruby
104
+ require 'mojo_logger'
105
+
106
+ MojoLogger.config do |config|
107
+ config.custom_line("log4j.logger.org.apache.zookeeper.ClientCnxn=INFO")
108
+ end
109
+
110
+ ```
111
+
112
+ #### Add Custom adapter for format your logs
113
+ Add an adapter that will accept whatever fields you want to input and output a hash.
114
+ This hash will be converted to json by MojoLogger.
115
+
116
+ ```ruby
117
+ require 'mojo_logger'
118
+ include MojoLogger
119
+
120
+ ENV['RACK_ENV'] = 'development'
121
+
122
+ # by default you use the standard adapter
123
+
124
+ mojo_debug({}, 'category', 'this is my message', { custom: :field })
125
+ # => {"time":"04-01-2016 22:25:33.019 +0000","app":"Mojo","env":"development","session_id":null,"reference_id":null,"api":null,"category":"category","message":"this is my message","custom":"field"}
126
+
127
+ class CustomLogAdapter
128
+ # an adapter must repond to #format() and return a Hash
129
+ def format(message)
130
+ {
131
+ 'message' => message,
132
+ 'constant' => 'Hello!'
133
+ }
134
+ end
135
+ end
136
+
137
+
138
+ MojoLogger.config do |config|
139
+ config.adapter = CustomLogAdapter.new
140
+ end
141
+
142
+ # now you can call the #mojo_* methods with the parameter you define
143
+
144
+ mojo_debug('my message')
145
+ # => {"time":"04-01-2016 22:26:43.441 +0000","app":"Mojo","env":"development","message":"my message","constant":"Hello!"}
146
+
147
+ ```
148
+
26
149
 
27
150
  ## Development
28
151
 
data/bench/gen.rb ADDED
@@ -0,0 +1,27 @@
1
+ require 'pry'
2
+ require 'benchmark'
3
+ require 'mojo_logger'
4
+
5
+ MojoLogger.config do |c|
6
+ c.default_log_level= :info
7
+ c.log_file = '/dev/null'
8
+ end
9
+
10
+ @mojo_args = [{}, "Category", "Test Message", { key: "This is a string", key2: "this is some other thing", 'key2' => "Third thing" }]
11
+
12
+ 5.times do
13
+
14
+ Benchmark.bmbm do |x|
15
+ x.report("debug") { 100000.times { MojoLogger.debug "Log message" } }
16
+ x.report("info") { 100000.times { MojoLogger.info "Log message" } }
17
+ end
18
+
19
+
20
+ Benchmark.bmbm do |x|
21
+ x.report("mojo_debug") { 100000.times { MojoLogger.mojo_debug(*@mojo_args) } }
22
+ x.report("mojo_info") { 100000.times { MojoLogger.mojo_info(*@mojo_args) } }
23
+ end
24
+
25
+ end
26
+
27
+
@@ -0,0 +1,80 @@
1
+ Rehearsal -----------------------------------------
2
+ debug 1.420000 0.040000 1.460000 ( 0.446000)
3
+ info 1.160000 0.050000 1.210000 ( 0.416000)
4
+ -------------------------------- total: 2.670000sec
5
+
6
+ user system total real
7
+ debug 0.280000 0.010000 0.290000 ( 0.117000)
8
+ info 0.120000 0.050000 0.170000 ( 0.166000)
9
+ Rehearsal ----------------------------------------------
10
+ mojo_debug 0.360000 0.010000 0.370000 ( 0.107000)
11
+ mojo_info 6.740000 0.070000 6.810000 ( 2.873000)
12
+ ------------------------------------- total: 7.180000sec
13
+
14
+ user system total real
15
+ mojo_debug 0.020000 0.000000 0.020000 ( 0.022000)
16
+ mojo_info 1.880000 0.030000 1.910000 ( 1.871000)
17
+ Rehearsal -----------------------------------------
18
+ debug 0.080000 0.000000 0.080000 ( 0.073000)
19
+ info 0.160000 0.030000 0.190000 ( 0.175000)
20
+ -------------------------------- total: 0.270000sec
21
+
22
+ user system total real
23
+ debug 0.080000 0.000000 0.080000 ( 0.075000)
24
+ info 0.150000 0.010000 0.160000 ( 0.171000)
25
+ Rehearsal ----------------------------------------------
26
+ mojo_debug 0.020000 0.000000 0.020000 ( 0.021000)
27
+ mojo_info 1.820000 0.040000 1.860000 ( 1.803000)
28
+ ------------------------------------- total: 1.880000sec
29
+
30
+ user system total real
31
+ mojo_debug 0.020000 0.000000 0.020000 ( 0.018000)
32
+ mojo_info 1.870000 0.020000 1.890000 ( 1.872000)
33
+ Rehearsal -----------------------------------------
34
+ debug 0.070000 0.000000 0.070000 ( 0.065000)
35
+ info 0.150000 0.010000 0.160000 ( 0.160000)
36
+ -------------------------------- total: 0.230000sec
37
+
38
+ user system total real
39
+ debug 0.080000 0.000000 0.080000 ( 0.069000)
40
+ info 0.150000 0.020000 0.170000 ( 0.164000)
41
+ Rehearsal ----------------------------------------------
42
+ mojo_debug 0.020000 0.000000 0.020000 ( 0.018000)
43
+ mojo_info 1.850000 0.020000 1.870000 ( 1.820000)
44
+ ------------------------------------- total: 1.890000sec
45
+
46
+ user system total real
47
+ mojo_debug 0.030000 0.000000 0.030000 ( 0.020000)
48
+ mojo_info 1.840000 0.030000 1.870000 ( 1.824000)
49
+ Rehearsal -----------------------------------------
50
+ debug 0.080000 0.000000 0.080000 ( 0.067000)
51
+ info 0.150000 0.010000 0.160000 ( 0.157000)
52
+ -------------------------------- total: 0.240000sec
53
+
54
+ user system total real
55
+ debug 0.060000 0.000000 0.060000 ( 0.068000)
56
+ info 0.160000 0.010000 0.170000 ( 0.157000)
57
+ Rehearsal ----------------------------------------------
58
+ mojo_debug 0.040000 0.000000 0.040000 ( 0.022000)
59
+ mojo_info 1.760000 0.020000 1.780000 ( 1.758000)
60
+ ------------------------------------- total: 1.820000sec
61
+
62
+ user system total real
63
+ mojo_debug 0.030000 0.000000 0.030000 ( 0.022000)
64
+ mojo_info 1.940000 0.010000 1.950000 ( 1.893000)
65
+ Rehearsal -----------------------------------------
66
+ debug 0.070000 0.000000 0.070000 ( 0.072000)
67
+ info 0.180000 0.010000 0.190000 ( 0.173000)
68
+ -------------------------------- total: 0.260000sec
69
+
70
+ user system total real
71
+ debug 0.070000 0.000000 0.070000 ( 0.075000)
72
+ info 0.160000 0.020000 0.180000 ( 0.174000)
73
+ Rehearsal ----------------------------------------------
74
+ mojo_debug 0.020000 0.000000 0.020000 ( 0.021000)
75
+ mojo_info 1.940000 0.030000 1.970000 ( 1.941000)
76
+ ------------------------------------- total: 1.990000sec
77
+
78
+ user system total real
79
+ mojo_debug 0.020000 0.000000 0.020000 ( 0.020000)
80
+ mojo_info 1.840000 0.020000 1.860000 ( 1.819000)
data/bench/results.txt ADDED
@@ -0,0 +1,80 @@
1
+ Rehearsal -----------------------------------------
2
+ debug 1.040000 0.000000 1.040000 ( 0.313000)
3
+ info 1.020000 0.020000 1.040000 ( 0.333000)
4
+ -------------------------------- total: 2.080000sec
5
+
6
+ user system total real
7
+ debug 0.250000 0.000000 0.250000 ( 0.101000)
8
+ info 0.390000 0.010000 0.400000 ( 0.196000)
9
+ Rehearsal ----------------------------------------------
10
+ mojo_debug 5.740000 0.060000 5.800000 ( 2.513000)
11
+ mojo_info 1.850000 0.030000 1.880000 ( 1.632000)
12
+ ------------------------------------- total: 7.680000sec
13
+
14
+ user system total real
15
+ mojo_debug 1.530000 0.000000 1.530000 ( 1.425000)
16
+ mojo_info 1.620000 0.030000 1.650000 ( 1.591000)
17
+ Rehearsal -----------------------------------------
18
+ debug 0.070000 0.000000 0.070000 ( 0.062000)
19
+ info 0.260000 0.030000 0.290000 ( 0.187000)
20
+ -------------------------------- total: 0.360000sec
21
+
22
+ user system total real
23
+ debug 0.060000 0.000000 0.060000 ( 0.060000)
24
+ info 0.130000 0.020000 0.150000 ( 0.149000)
25
+ Rehearsal ----------------------------------------------
26
+ mojo_debug 1.460000 0.000000 1.460000 ( 1.419000)
27
+ mojo_info 1.630000 0.030000 1.660000 ( 1.615000)
28
+ ------------------------------------- total: 3.120000sec
29
+
30
+ user system total real
31
+ mojo_debug 1.460000 0.010000 1.470000 ( 1.424000)
32
+ mojo_info 1.620000 0.030000 1.650000 ( 1.607000)
33
+ Rehearsal -----------------------------------------
34
+ debug 0.060000 0.000000 0.060000 ( 0.062000)
35
+ info 0.130000 0.020000 0.150000 ( 0.145000)
36
+ -------------------------------- total: 0.210000sec
37
+
38
+ user system total real
39
+ debug 0.070000 0.000000 0.070000 ( 0.062000)
40
+ info 0.140000 0.000000 0.140000 ( 0.147000)
41
+ Rehearsal ----------------------------------------------
42
+ mojo_debug 1.440000 0.010000 1.450000 ( 1.411000)
43
+ mojo_info 1.590000 0.030000 1.620000 ( 1.602000)
44
+ ------------------------------------- total: 3.070000sec
45
+
46
+ user system total real
47
+ mojo_debug 1.540000 0.000000 1.540000 ( 1.423000)
48
+ mojo_info 1.610000 0.030000 1.640000 ( 1.607000)
49
+ Rehearsal -----------------------------------------
50
+ debug 0.060000 0.000000 0.060000 ( 0.061000)
51
+ info 0.130000 0.020000 0.150000 ( 0.145000)
52
+ -------------------------------- total: 0.210000sec
53
+
54
+ user system total real
55
+ debug 0.070000 0.000000 0.070000 ( 0.061000)
56
+ info 0.120000 0.020000 0.140000 ( 0.147000)
57
+ Rehearsal ----------------------------------------------
58
+ mojo_debug 1.480000 0.000000 1.480000 ( 1.413000)
59
+ mojo_info 1.640000 0.020000 1.660000 ( 1.613000)
60
+ ------------------------------------- total: 3.140000sec
61
+
62
+ user system total real
63
+ mojo_debug 1.440000 0.000000 1.440000 ( 1.409000)
64
+ mojo_info 1.610000 0.030000 1.640000 ( 1.608000)
65
+ Rehearsal -----------------------------------------
66
+ debug 0.060000 0.000000 0.060000 ( 0.060000)
67
+ info 0.140000 0.010000 0.150000 ( 0.144000)
68
+ -------------------------------- total: 0.210000sec
69
+
70
+ user system total real
71
+ debug 0.070000 0.000000 0.070000 ( 0.062000)
72
+ info 0.130000 0.010000 0.140000 ( 0.145000)
73
+ Rehearsal ----------------------------------------------
74
+ mojo_debug 1.440000 0.010000 1.450000 ( 1.414000)
75
+ mojo_info 1.620000 0.030000 1.650000 ( 1.608000)
76
+ ------------------------------------- total: 3.100000sec
77
+
78
+ user system total real
79
+ mojo_debug 1.430000 0.010000 1.440000 ( 1.408000)
80
+ mojo_info 1.610000 0.020000 1.630000 ( 1.609000)
File without changes
data/lib/mojo_logger.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require_relative 'mojo_logger/version'
2
2
  require_relative 'mojo_logger/mojo_logger'
3
+ require_relative 'mojo_logger/default_adapter'
3
4
  require_relative 'mojo_logger/configurator'
4
5
  require_relative 'mojo_logger/appender'
5
6
 
@@ -24,19 +24,22 @@ module MojoLogger
24
24
  end
25
25
 
26
26
  def file=(file)
27
- #raise "FileNotFound" unless File.exist?(file)
28
27
  @type = :file
29
28
  @file = file #File.expand_path(file)
30
29
  end
31
30
 
32
31
  def generate_properties_string
33
32
  properties = generate_generic_properties
34
- properties += generate_file_properties if @file
33
+ properties << generate_file_properties if @file
35
34
  properties
36
35
  end
37
36
 
37
+
38
+
38
39
  private
39
40
 
41
+
42
+
40
43
  def valid_log_level?(lvl)
41
44
  ACCEPTABLE_LOG_LEVELS.include?(lvl)
42
45
  end
@@ -45,7 +48,7 @@ module MojoLogger
45
48
  @type = :console
46
49
  @max_file_size = opts[:max_file_size] || '10MB'
47
50
  @max_backup_index = opts[:max_backup_index] || '10'
48
- @pattern = opts[:pattern] || '%n %m %n'
51
+ @pattern = opts[:pattern] || '%m %n'
49
52
 
50
53
  self.level=(opts[:level]) if opts[:level]
51
54
  self.file=(opts[:file]) if opts[:file]
@@ -57,16 +60,16 @@ module MojoLogger
57
60
 
58
61
  def generate_generic_properties
59
62
  properties = "#{property_header}=#{log4j_type}\n"
60
- properties += "#{property_header}.layout=org.apache.log4j.PatternLayout\n"
61
- properties += "#{property_header}.layout.ConversionPattern=#{@pattern}\n"
62
- properties += "#{property_header}.Threshold=#{@level}\n" if @level
63
+ properties << "#{property_header}.layout=org.apache.log4j.PatternLayout\n"
64
+ properties << "#{property_header}.layout.ConversionPattern=#{@pattern}\n"
65
+ properties << "#{property_header}.Threshold=#{@level}\n" if @level
63
66
  properties
64
67
  end
65
68
 
66
69
  def generate_file_properties
67
70
  properties = "#{property_header}.file=#{@file}\n"
68
- properties += "#{property_header}.MaxFileSize=#{@max_file_size}\n"
69
- properties += "#{property_header}.MaxBackupIndex=#{@max_backup_index}\n"
71
+ properties << "#{property_header}.MaxFileSize=#{@max_file_size}\n"
72
+ properties << "#{property_header}.MaxBackupIndex=#{@max_backup_index}\n"
70
73
  properties
71
74
  end
72
75
 
@@ -1,20 +1,29 @@
1
-
1
+ require 'forwardable'
2
2
 
3
3
  module MojoLogger
4
4
  class Configurator
5
- # stringio = StringIO.new(s)
6
- # jstrinio = org.jruby.util.IOInputStream.new(stringio)
7
- # logger = Java::org.apache.log4j.PropertyConfigurator.configure(jstringio)
8
- # logger = Java::org.apache.log4j.Logger.getLogger('Mojo')
9
- #
5
+ extend Forwardable
6
+
7
+ ENV_MAPPING = {
8
+ 'development' => 'dev',
9
+ 'stage' => 'stg',
10
+ 'production' => 'prd'
11
+ }
10
12
 
11
- attr_reader :properties_file
13
+ attr_reader :properties_file, :env
14
+ attr_accessor :application_name
15
+ def_delegators :@default_appender, :pattern, :pattern=
16
+ def_delegators :@default_appender, :max_file_size, :max_file_size=
17
+ def_delegators :@default_appender, :max_backup_index, :max_backup_index=
12
18
 
13
19
  def initialize()
14
- @default_appender = MojoLogger::Appender.new("MojoLogger")
20
+ @default_appender = MojoLogger::Appender.new("MojoLogger")
15
21
  @default_appender.level = "DEBUG"
16
- @use_default_appender = true
17
- @appenders = []
22
+ @use_default_appender = true
23
+ @appenders = []
24
+ @application_name = "Mojo"
25
+ @adapter = DefaultAdapter.new
26
+ @env = environment
18
27
  end
19
28
 
20
29
  def default_log_level
@@ -47,11 +56,20 @@ module MojoLogger
47
56
 
48
57
  def generate_properties_string
49
58
  properties = "#{generate_root_logger_line}\n"
50
- properties += "#{generate_custom_lines}\n"
51
- properties += "#{generate_appender_lines}\n"
59
+ properties << "#{generate_custom_lines}\n"
60
+ properties << "#{generate_appender_lines}\n"
52
61
  properties
53
62
  end
54
63
 
64
+
65
+ def adapter=(adapter)
66
+ @adapter = adapter
67
+ end
68
+
69
+ def adapter
70
+ @adapter
71
+ end
72
+
55
73
  private
56
74
 
57
75
  def generate_root_logger_line
@@ -70,11 +88,16 @@ module MojoLogger
70
88
 
71
89
  def generate_appender_lines
72
90
  lines = ''
73
- lines += @default_appender.generate_properties_string if @use_default_appender
74
- lines += "#{@appenders.map { |a| a.generate_properties_string }.join("\n") }"
91
+ lines << @default_appender.generate_properties_string if @use_default_appender
92
+ lines << "#{@appenders.map { |a| a.generate_properties_string }.join("\n") }"
75
93
 
76
94
  lines
77
95
  end
78
96
 
97
+ def environment
98
+ env_var = ENV['RAILS_ENV'] || ENV['RACK_ENV']
99
+ ENV_MAPPING.fetch(env_var, env_var)
100
+ end
101
+
79
102
  end
80
103
  end
@@ -0,0 +1,29 @@
1
+ module MojoLogger
2
+ class DefaultAdapter
3
+
4
+ def format(api_request, category, message, options=nil)
5
+ msg = {
6
+ 'session_id' => api_request[:session_id],
7
+ 'reference_id' => api_request[:reference_id],
8
+ 'api' => api_request[:api],
9
+ 'category' => category,
10
+ 'message' => message
11
+ }
12
+
13
+ msg.merge!(process_options(options)) if options
14
+ msg
15
+ end
16
+
17
+ private
18
+
19
+ def process_options(options)
20
+ if options.is_a?(Hash)
21
+ options
22
+ else
23
+ { 'options' => options }
24
+ end
25
+ end
26
+
27
+ end
28
+ end
29
+
@@ -1,9 +1,17 @@
1
- require 'java'
2
1
  require 'time'
3
2
  require 'json'
4
- require_relative '../../jars/log4j-1.2.17.jar'
3
+ require 'stringio'
4
+ require 'forwardable'
5
+
6
+ # Making this work with mri ruby
7
+ require 'java' if RUBY_PLATFORM == 'java'
8
+ require_relative '../jars/log4j-1.2.17.jar' if RUBY_PLATFORM == 'java'
9
+
10
+ require 'logger' if RUBY_PLATFORM != 'java'
5
11
 
6
12
  module MojoLogger
13
+ extend SingleForwardable
14
+ def_delegators :logger, :debug, :info, :warn, :error, :fatal
7
15
 
8
16
  def mojo_debug(*args)
9
17
  MojoLogger.mojo_debug(*args)
@@ -21,68 +29,116 @@ module MojoLogger
21
29
  MojoLogger.mojo_error(*args)
22
30
  end
23
31
 
32
+ def mojo_fatal(*args)
33
+ MojoLogger.mojo_fatal(*args)
34
+ end
35
+
24
36
  def logger
25
37
  MojoLogger.logger
26
38
  end
27
39
 
40
+ def level
41
+ MojoLogger.level
42
+ end
43
+
28
44
  class << self
29
45
 
30
46
  def logger
31
47
  @@logger ||= configure_logger
32
48
  end
33
49
 
34
- def mojo_msg(api_request, category, message, options=nil)
50
+ def mojo_msg(*args)
35
51
  msg = {
36
52
  'time' => Time.now.utc.strftime("%m-%d-%Y %H:%M:%S.%L %z"),
37
- 'app' => "Mojo",
38
- 'session_id' => api_request[:session_id],
39
- 'reference_id' => api_request[:reference_id],
40
- 'api' => api_request[:api],
41
- 'category' => category,
42
- 'message' => message
43
- }
44
-
45
- msg.merge!(options) if options
53
+ 'app' => configurator.application_name,
54
+ 'env' => configurator.env
55
+ }.merge!(configurator.adapter.format(*args))
56
+
46
57
  msg.to_json
47
58
  end
48
59
 
49
60
  def mojo_debug(*args)
50
- logger.debug(mojo_msg(*args))
61
+ if level == :debug
62
+ logger.debug(mojo_msg(*args))
63
+ end
51
64
  end
52
65
 
53
66
  def mojo_info(*args)
54
- logger.info(mojo_msg(*args))
67
+ if level == :debug || level == :info
68
+ logger.info(mojo_msg(*args))
69
+ end
55
70
  end
56
71
 
57
72
  def mojo_warn(*args)
58
- logger.warn(mojo_msg(*args))
73
+ unless level == :error || level == :fatal
74
+ logger.warn(mojo_msg(*args))
75
+ end
59
76
  end
60
77
 
61
78
  def mojo_error(*args)
62
- logger.error(mojo_msg(*args))
79
+ unless level == :fatal
80
+ logger.error(mojo_msg(*args))
81
+ end
63
82
  end
64
83
 
84
+ def mojo_fatal(*args)
85
+ logger.fatal(mojo_msg(*args))
86
+ end
65
87
 
66
- def config
67
- @@config = MojoLogger::Configurator.new
88
+ def default_log_level
89
+ configurator.default_log_level
90
+ end
68
91
 
69
- yield(@@config) if block_given?
92
+ def level
93
+ @level ||= configurator.default_log_level.downcase.to_sym
94
+ end
70
95
 
96
+ def level=(lvl)
97
+ configurator.default_log_level = lvl
71
98
  @@logger = configure_logger
72
- @@config
99
+ @level = configurator.default_log_level.downcase.to_sym
73
100
  end
74
101
 
102
+ def config
103
+ if block_given?
104
+ @@config = MojoLogger::Configurator.new
105
+ yield(@@config)
106
+ @@logger = configure_logger
107
+ end
108
+
109
+ @level = configurator.default_log_level.downcase.to_sym
110
+ configurator
111
+ end
75
112
 
76
- private
113
+ private
77
114
 
78
- def configure_logger
115
+ def configurator
79
116
  @@config ||= MojoLogger::Configurator.new
117
+ end
80
118
 
81
- stringio = StringIO.new(@@config.generate_properties_string)
82
- java_stringio = org.jruby.util.IOInputStream.new(stringio)
119
+ def configure_logger
120
+ if RUBY_PLATFORM == 'java'
121
+ stringio = StringIO.new(configurator.generate_properties_string)
122
+ java_stringio = org.jruby.util.IOInputStream.new(stringio)
123
+
124
+ Java::org.apache.log4j.PropertyConfigurator.configure(java_stringio)
125
+ Java::org.apache.log4j.Logger.getLogger('MojoLogger')
126
+ else
127
+ l = Logger.new(STDOUT)
128
+ l.level = configurator.default_log_level.downcase.to_sym
129
+ l
130
+ end
131
+ end
83
132
 
84
- Java::org.apache.log4j.PropertyConfigurator.configure(java_stringio)
85
- Java::org.apache.log4j.Logger.getLogger('MojoLogger')
133
+ def process_options(options=nil)
134
+ case options
135
+ when Hash
136
+ options
137
+ when String
138
+ { 'options' => options }
139
+ else
140
+ nil
141
+ end
86
142
  end
87
143
 
88
144
  end # End 'class' methods
@@ -1,3 +1,3 @@
1
1
  module MojoLogger
2
- VERSION = "0.1.0"
2
+ VERSION = "0.6.0"
3
3
  end
data/mojo_logger.gemspec CHANGED
@@ -22,11 +22,10 @@ Gem::Specification.new do |spec|
22
22
  spec.license = "MIT"
23
23
 
24
24
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
- spec.bindir = "exe"
26
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
25
  spec.require_paths = ["lib"]
28
26
 
29
27
  spec.add_development_dependency "bundler", "~> 1.8"
30
28
  spec.add_development_dependency "rake", "~> 10.0"
31
29
  spec.add_development_dependency 'rspec'
30
+ spec.add_development_dependency 'pry'
32
31
  end
metadata CHANGED
@@ -1,57 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mojo_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Thomas
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-24 00:00:00.000000000 Z
11
+ date: 2016-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '1.8'
14
19
  name: bundler
20
+ prerelease: false
21
+ type: :development
15
22
  version_requirements: !ruby/object:Gem::Requirement
16
23
  requirements:
17
- - - ~>
24
+ - - "~>"
18
25
  - !ruby/object:Gem::Version
19
26
  version: '1.8'
27
+ - !ruby/object:Gem::Dependency
20
28
  requirement: !ruby/object:Gem::Requirement
21
29
  requirements:
22
- - - ~>
30
+ - - "~>"
23
31
  - !ruby/object:Gem::Version
24
- version: '1.8'
32
+ version: '10.0'
33
+ name: rake
25
34
  prerelease: false
26
35
  type: :development
27
- - !ruby/object:Gem::Dependency
28
- name: rake
29
36
  version_requirements: !ruby/object:Gem::Requirement
30
37
  requirements:
31
- - - ~>
38
+ - - "~>"
32
39
  - !ruby/object:Gem::Version
33
40
  version: '10.0'
41
+ - !ruby/object:Gem::Dependency
34
42
  requirement: !ruby/object:Gem::Requirement
35
43
  requirements:
36
- - - ~>
44
+ - - ">="
37
45
  - !ruby/object:Gem::Version
38
- version: '10.0'
46
+ version: '0'
47
+ name: rspec
39
48
  prerelease: false
40
49
  type: :development
41
- - !ruby/object:Gem::Dependency
42
- name: rspec
43
50
  version_requirements: !ruby/object:Gem::Requirement
44
51
  requirements:
45
- - - '>='
52
+ - - ">="
46
53
  - !ruby/object:Gem::Version
47
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
48
56
  requirement: !ruby/object:Gem::Requirement
49
57
  requirements:
50
- - - '>='
58
+ - - ">="
51
59
  - !ruby/object:Gem::Version
52
60
  version: '0'
61
+ name: pry
53
62
  prerelease: false
54
63
  type: :development
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description: |2
56
70
  This gem is a wrapper around log4j that allow you to
57
71
  construct a log4j.properties file from a config block, rather than actually
@@ -62,20 +76,25 @@ executables: []
62
76
  extensions: []
63
77
  extra_rdoc_files: []
64
78
  files:
65
- - .gitignore
66
- - .rspec
67
- - .travis.yml
79
+ - ".gitignore"
80
+ - ".rspec"
81
+ - ".ruby-version"
82
+ - ".travis.yml"
68
83
  - Gemfile
69
84
  - LICENSE.txt
70
85
  - README.md
71
86
  - Rakefile
87
+ - bench/gen.rb
88
+ - bench/results-optimize-4-apr-2016.txt
89
+ - bench/results.txt
72
90
  - bin/console
73
91
  - bin/setup
74
92
  - conf/log4j.properties
75
- - jars/log4j-1.2.17.jar
93
+ - lib/jars/log4j-1.2.17.jar
76
94
  - lib/mojo_logger.rb
77
95
  - lib/mojo_logger/appender.rb
78
96
  - lib/mojo_logger/configurator.rb
97
+ - lib/mojo_logger/default_adapter.rb
79
98
  - lib/mojo_logger/mojo_logger.rb
80
99
  - lib/mojo_logger/version.rb
81
100
  - mojo_logger.gemspec
@@ -89,17 +108,17 @@ require_paths:
89
108
  - lib
90
109
  required_ruby_version: !ruby/object:Gem::Requirement
91
110
  requirements:
92
- - - '>='
111
+ - - ">="
93
112
  - !ruby/object:Gem::Version
94
113
  version: '0'
95
114
  required_rubygems_version: !ruby/object:Gem::Requirement
96
115
  requirements:
97
- - - '>='
116
+ - - ">="
98
117
  - !ruby/object:Gem::Version
99
118
  version: '0'
100
119
  requirements: []
101
120
  rubyforge_project:
102
- rubygems_version: 2.4.6
121
+ rubygems_version: 2.6.4
103
122
  signing_key:
104
123
  specification_version: 4
105
124
  summary: log4j wrapper that handles logging in the Mojo framework.