mojo_logger 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/conf/log4j.properties +11 -0
- data/jars/log4j-1.2.17.jar +0 -0
- data/lib/mojo_logger/appender.rb +82 -0
- data/lib/mojo_logger/configurator.rb +80 -0
- data/lib/mojo_logger/mojo_logger.rb +90 -0
- data/lib/mojo_logger/version.rb +3 -0
- data/lib/mojo_logger.rb +7 -0
- data/mojo_logger.gemspec +32 -0
- metadata +106 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: dc5077befcd1a3215bfc2bc080e94e1ab3810479
|
4
|
+
data.tar.gz: f63f06a080617ba976d3b8270b3809d6ed58bf9f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 14b21d9122cebc7b95d94a577c4dc761ade4abfc6ab78b54b357a51f854ede05afb05ee31fa71872aa92ef81e471673b8fae61e81035bf0d43bf66e492019087
|
7
|
+
data.tar.gz: e84d470a60e7d395b2e9b938957ca80a6cffbe4b56392075f141de37a21b399d150b50204e99c8c41e83d139e4ef290bb58a34183298528ee7665fbb28c4267d
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Autodesk, Inc.
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# MojoLogger
|
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.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'mojo_logger'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install mojo_logger
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
1. Fork it ( https://github.com/[my-github-username]/mojo_logger/fork )
|
36
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
38
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "mojo_logger"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
log4j.rootLogger = INFO, CONSOLE
|
2
|
+
|
3
|
+
######################## Add CONSOLE APPENDER ##############################
|
4
|
+
######## Comment this section out to suppress local console logging ########
|
5
|
+
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
|
6
|
+
log4j.appender.CONSOLE.Threshold=DEBUG
|
7
|
+
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
|
8
|
+
log4j.appender.CONSOLE.layout.ConversionPattern={ "time": "%d{MM-dd-yyyy HH:mm:ss.SSS XX}", "log": %m } %n
|
9
|
+
##############################################################################
|
10
|
+
|
11
|
+
|
Binary file
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module MojoLogger
|
2
|
+
class Appender
|
3
|
+
|
4
|
+
ACCEPTABLE_LOG_LEVELS = [
|
5
|
+
"DEBUG",
|
6
|
+
"INFO",
|
7
|
+
"WARN",
|
8
|
+
"ERROR",
|
9
|
+
"FATAL"
|
10
|
+
]
|
11
|
+
|
12
|
+
attr_reader :name, :level, :file
|
13
|
+
attr_accessor :pattern, :max_file_size, :max_backup_index
|
14
|
+
|
15
|
+
def initialize(name, opts={})
|
16
|
+
@name = name
|
17
|
+
set_defaults(opts)
|
18
|
+
end
|
19
|
+
|
20
|
+
def level=(lvl)
|
21
|
+
l = lvl.to_s.upcase
|
22
|
+
raise "InvalidLogLevel" unless valid_log_level?(l)
|
23
|
+
@level = l
|
24
|
+
end
|
25
|
+
|
26
|
+
def file=(file)
|
27
|
+
#raise "FileNotFound" unless File.exist?(file)
|
28
|
+
@type = :file
|
29
|
+
@file = file #File.expand_path(file)
|
30
|
+
end
|
31
|
+
|
32
|
+
def generate_properties_string
|
33
|
+
properties = generate_generic_properties
|
34
|
+
properties += generate_file_properties if @file
|
35
|
+
properties
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def valid_log_level?(lvl)
|
41
|
+
ACCEPTABLE_LOG_LEVELS.include?(lvl)
|
42
|
+
end
|
43
|
+
|
44
|
+
def set_defaults(opts={})
|
45
|
+
@type = :console
|
46
|
+
@max_file_size = opts[:max_file_size] || '10MB'
|
47
|
+
@max_backup_index = opts[:max_backup_index] || '10'
|
48
|
+
@pattern = opts[:pattern] || '%n %m %n'
|
49
|
+
|
50
|
+
self.level=(opts[:level]) if opts[:level]
|
51
|
+
self.file=(opts[:file]) if opts[:file]
|
52
|
+
end
|
53
|
+
|
54
|
+
def property_header
|
55
|
+
"log4j.appender.#{@name}"
|
56
|
+
end
|
57
|
+
|
58
|
+
def generate_generic_properties
|
59
|
+
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
|
64
|
+
end
|
65
|
+
|
66
|
+
def generate_file_properties
|
67
|
+
properties = "#{property_header}.file=#{@file}\n"
|
68
|
+
properties += "#{property_header}.MaxFileSize=#{@max_file_size}\n"
|
69
|
+
properties += "#{property_header}.MaxBackupIndex=#{@max_backup_index}\n"
|
70
|
+
properties
|
71
|
+
end
|
72
|
+
|
73
|
+
def log4j_type
|
74
|
+
if @type == :file
|
75
|
+
"org.apache.log4j.RollingFileAppender"
|
76
|
+
else
|
77
|
+
"org.apache.log4j.ConsoleAppender"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
module MojoLogger
|
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
|
+
#
|
10
|
+
|
11
|
+
attr_reader :properties_file
|
12
|
+
|
13
|
+
def initialize()
|
14
|
+
@default_appender = MojoLogger::Appender.new("MojoLogger")
|
15
|
+
@default_appender.level = "DEBUG"
|
16
|
+
@use_default_appender = true
|
17
|
+
@appenders = []
|
18
|
+
end
|
19
|
+
|
20
|
+
def default_log_level
|
21
|
+
@default_appender.level
|
22
|
+
end
|
23
|
+
|
24
|
+
def default_log_level=(lvl)
|
25
|
+
@default_appender.level = lvl
|
26
|
+
end
|
27
|
+
|
28
|
+
def log_file=(file)
|
29
|
+
@default_appender.file = file
|
30
|
+
end
|
31
|
+
|
32
|
+
def properties_file=(file)
|
33
|
+
raise "FileNotFound" unless File.exist?(file)
|
34
|
+
@type = :file
|
35
|
+
@properties_file = File.expand_path(file)
|
36
|
+
end
|
37
|
+
|
38
|
+
def custom_line(line)
|
39
|
+
@custom_lines ||= []
|
40
|
+
@custom_lines << line
|
41
|
+
end
|
42
|
+
|
43
|
+
def add_appender(appender)
|
44
|
+
raise "InvalidAppender" unless appender.is_a?(MojoLogger::Appender)
|
45
|
+
@appenders << appender
|
46
|
+
end
|
47
|
+
|
48
|
+
def generate_properties_string
|
49
|
+
properties = "#{generate_root_logger_line}\n"
|
50
|
+
properties += "#{generate_custom_lines}\n"
|
51
|
+
properties += "#{generate_appender_lines}\n"
|
52
|
+
properties
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def generate_root_logger_line
|
58
|
+
"log4j.rootLogger=#{default_log_level}#{list_appenders}"
|
59
|
+
end
|
60
|
+
|
61
|
+
def list_appenders
|
62
|
+
names = @appenders.map { |a| a.name }
|
63
|
+
names.unshift(@default_appender.name) if @use_default_appender
|
64
|
+
", #{names.join(',')}"
|
65
|
+
end
|
66
|
+
|
67
|
+
def generate_custom_lines
|
68
|
+
"#{@custom_lines.join("\n") if @custom_lines}"
|
69
|
+
end
|
70
|
+
|
71
|
+
def generate_appender_lines
|
72
|
+
lines = ''
|
73
|
+
lines += @default_appender.generate_properties_string if @use_default_appender
|
74
|
+
lines += "#{@appenders.map { |a| a.generate_properties_string }.join("\n") }"
|
75
|
+
|
76
|
+
lines
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'java'
|
2
|
+
require 'time'
|
3
|
+
require 'json'
|
4
|
+
require_relative '../../jars/log4j-1.2.17.jar'
|
5
|
+
|
6
|
+
module MojoLogger
|
7
|
+
|
8
|
+
def mojo_debug(*args)
|
9
|
+
MojoLogger.mojo_debug(*args)
|
10
|
+
end
|
11
|
+
|
12
|
+
def mojo_info(*args)
|
13
|
+
MojoLogger.mojo_info(*args)
|
14
|
+
end
|
15
|
+
|
16
|
+
def mojo_warn(*args)
|
17
|
+
MojoLogger.mojo_warn(*args)
|
18
|
+
end
|
19
|
+
|
20
|
+
def mojo_error(*args)
|
21
|
+
MojoLogger.mojo_error(*args)
|
22
|
+
end
|
23
|
+
|
24
|
+
def logger
|
25
|
+
MojoLogger.logger
|
26
|
+
end
|
27
|
+
|
28
|
+
class << self
|
29
|
+
|
30
|
+
def logger
|
31
|
+
@@logger ||= configure_logger
|
32
|
+
end
|
33
|
+
|
34
|
+
def mojo_msg(api_request, category, message, options=nil)
|
35
|
+
msg = {
|
36
|
+
'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
|
46
|
+
msg.to_json
|
47
|
+
end
|
48
|
+
|
49
|
+
def mojo_debug(*args)
|
50
|
+
logger.debug(mojo_msg(*args))
|
51
|
+
end
|
52
|
+
|
53
|
+
def mojo_info(*args)
|
54
|
+
logger.info(mojo_msg(*args))
|
55
|
+
end
|
56
|
+
|
57
|
+
def mojo_warn(*args)
|
58
|
+
logger.warn(mojo_msg(*args))
|
59
|
+
end
|
60
|
+
|
61
|
+
def mojo_error(*args)
|
62
|
+
logger.error(mojo_msg(*args))
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
def config
|
67
|
+
@@config = MojoLogger::Configurator.new
|
68
|
+
|
69
|
+
yield(@@config) if block_given?
|
70
|
+
|
71
|
+
@@logger = configure_logger
|
72
|
+
@@config
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
def configure_logger
|
79
|
+
@@config ||= MojoLogger::Configurator.new
|
80
|
+
|
81
|
+
stringio = StringIO.new(@@config.generate_properties_string)
|
82
|
+
java_stringio = org.jruby.util.IOInputStream.new(stringio)
|
83
|
+
|
84
|
+
Java::org.apache.log4j.PropertyConfigurator.configure(java_stringio)
|
85
|
+
Java::org.apache.log4j.Logger.getLogger('MojoLogger')
|
86
|
+
end
|
87
|
+
|
88
|
+
end # End 'class' methods
|
89
|
+
|
90
|
+
end
|
data/lib/mojo_logger.rb
ADDED
data/mojo_logger.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mojo_logger/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mojo_logger"
|
8
|
+
spec.version = MojoLogger::VERSION
|
9
|
+
spec.authors = ["John Thomas"]
|
10
|
+
spec.email = ["john.thomas@autodesk.com"]
|
11
|
+
|
12
|
+
#if spec.respond_to?(:metadata)
|
13
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com' to prevent pushes to rubygems.org, or delete to allow pushes to any server."
|
14
|
+
#end
|
15
|
+
|
16
|
+
spec.summary = "log4j wrapper that handles logging in the Mojo framework."
|
17
|
+
spec.description = %q{ This gem is a wrapper around log4j that allow you to
|
18
|
+
construct a log4j.properties file from a config block, rather than actually
|
19
|
+
having a log4j.properties file. Useful for setting the log levels dynamically.
|
20
|
+
}
|
21
|
+
spec.homepage = "https://github.com/thomas07vt/mojo_logger/"
|
22
|
+
spec.license = "MIT"
|
23
|
+
|
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
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
spec.add_development_dependency "bundler", "~> 1.8"
|
30
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
31
|
+
spec.add_development_dependency 'rspec'
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mojo_logger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- John Thomas
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
version_requirements: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.8'
|
20
|
+
requirement: !ruby/object:Gem::Requirement
|
21
|
+
requirements:
|
22
|
+
- - ~>
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: '1.8'
|
25
|
+
prerelease: false
|
26
|
+
type: :development
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ~>
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '10.0'
|
39
|
+
prerelease: false
|
40
|
+
type: :development
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
version_requirements: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
prerelease: false
|
54
|
+
type: :development
|
55
|
+
description: |2
|
56
|
+
This gem is a wrapper around log4j that allow you to
|
57
|
+
construct a log4j.properties file from a config block, rather than actually
|
58
|
+
having a log4j.properties file. Useful for setting the log levels dynamically.
|
59
|
+
email:
|
60
|
+
- john.thomas@autodesk.com
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- .gitignore
|
66
|
+
- .rspec
|
67
|
+
- .travis.yml
|
68
|
+
- Gemfile
|
69
|
+
- LICENSE.txt
|
70
|
+
- README.md
|
71
|
+
- Rakefile
|
72
|
+
- bin/console
|
73
|
+
- bin/setup
|
74
|
+
- conf/log4j.properties
|
75
|
+
- jars/log4j-1.2.17.jar
|
76
|
+
- lib/mojo_logger.rb
|
77
|
+
- lib/mojo_logger/appender.rb
|
78
|
+
- lib/mojo_logger/configurator.rb
|
79
|
+
- lib/mojo_logger/mojo_logger.rb
|
80
|
+
- lib/mojo_logger/version.rb
|
81
|
+
- mojo_logger.gemspec
|
82
|
+
homepage: https://github.com/thomas07vt/mojo_logger/
|
83
|
+
licenses:
|
84
|
+
- MIT
|
85
|
+
metadata: {}
|
86
|
+
post_install_message:
|
87
|
+
rdoc_options: []
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubyforge_project:
|
102
|
+
rubygems_version: 2.4.6
|
103
|
+
signing_key:
|
104
|
+
specification_version: 4
|
105
|
+
summary: log4j wrapper that handles logging in the Mojo framework.
|
106
|
+
test_files: []
|