json_logger 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ json_logger
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-1.9.3-p194
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.8.0"
10
+ gem "rdoc", "~> 3.12"
11
+ gem "bundler", "~> 1.0.0"
12
+ gem "jeweler", "~> 1.8.4"
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,63 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ addressable (2.3.5)
5
+ builder (3.2.2)
6
+ diff-lcs (1.1.3)
7
+ faraday (0.8.8)
8
+ multipart-post (~> 1.2.0)
9
+ git (1.2.6)
10
+ github_api (0.10.1)
11
+ addressable
12
+ faraday (~> 0.8.1)
13
+ hashie (>= 1.2)
14
+ multi_json (~> 1.4)
15
+ nokogiri (~> 1.5.2)
16
+ oauth2
17
+ hashie (2.0.5)
18
+ highline (1.6.19)
19
+ httpauth (0.2.0)
20
+ jeweler (1.8.7)
21
+ builder
22
+ bundler (~> 1.0)
23
+ git (>= 1.2.5)
24
+ github_api (= 0.10.1)
25
+ highline (>= 1.6.15)
26
+ nokogiri (= 1.5.10)
27
+ rake
28
+ rdoc
29
+ json (1.8.0)
30
+ jwt (0.1.8)
31
+ multi_json (>= 1.5)
32
+ multi_json (1.7.9)
33
+ multi_xml (0.5.5)
34
+ multipart-post (1.2.0)
35
+ nokogiri (1.5.10)
36
+ oauth2 (0.9.2)
37
+ faraday (~> 0.8)
38
+ httpauth (~> 0.2)
39
+ jwt (~> 0.1.4)
40
+ multi_json (~> 1.0)
41
+ multi_xml (~> 0.5)
42
+ rack (~> 1.2)
43
+ rack (1.5.2)
44
+ rake (10.1.0)
45
+ rdoc (3.12.2)
46
+ json (~> 1.4)
47
+ rspec (2.8.0)
48
+ rspec-core (~> 2.8.0)
49
+ rspec-expectations (~> 2.8.0)
50
+ rspec-mocks (~> 2.8.0)
51
+ rspec-core (2.8.0)
52
+ rspec-expectations (2.8.0)
53
+ diff-lcs (~> 1.1.2)
54
+ rspec-mocks (2.8.0)
55
+
56
+ PLATFORMS
57
+ ruby
58
+
59
+ DEPENDENCIES
60
+ bundler (~> 1.0.0)
61
+ jeweler (~> 1.8.4)
62
+ rdoc (~> 3.12)
63
+ rspec (~> 2.8.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Alex Dean
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,85 @@
1
+ ## JSON Logger
2
+
3
+ Simple structured logging.
4
+
5
+ A subclass of Ruby's standard `Logger` which makes it easy to write structured
6
+ data to your log files.
7
+
8
+ ## Basic Usage
9
+
10
+ ```ruby
11
+ instance = JsonLogger.factory(
12
+ :device => '/tmp/test.log', # anything understood by Logger.new
13
+ :level => Logger::INFO,
14
+ :progname => 'foo'
15
+ )
16
+ instance.info('a simple message')
17
+ instance.error(:key => 'value', :other => [1,2,3])
18
+ ```
19
+
20
+ Output in `/tmp/test.log`:
21
+ ```json
22
+ {
23
+ "time":"2013-08-28T20:54:42.048Z",
24
+ "progname":"foo",
25
+ "level":"INFO",
26
+ "level_id":1,
27
+ "msg":"a simple message"
28
+ }
29
+ {
30
+ "time":"2013-08-28T20:52:48.321Z",
31
+ "progname":"foo",
32
+ "level":"ERROR",
33
+ "level_id":3,
34
+ "data":"data",
35
+ "other":[1,2,3]
36
+ }
37
+ ```
38
+
39
+ Messages are actually written 1 per line (separated by a newline). I added the
40
+ extra newlines here for readability.
41
+
42
+ ## Factory Defaults
43
+
44
+ You can set some defaults which will be added to every `factory` call. This
45
+ works well from a Rails initializer, for example.
46
+
47
+ Subsequent calls to `factory` will always include these defaults, though they
48
+ can be overridden if desired.
49
+
50
+ ```ruby
51
+ JsonLogger.factory_defaults = {
52
+ :device => '/tmp/test.log',
53
+ :level => Logger::INFO
54
+ }
55
+ instance = JsonLogger.factory(:progname => 'foo')
56
+ instance.info 'a simple message'
57
+ ```
58
+
59
+ ## Presets
60
+
61
+ A `factory` call can include preset values which will be added to any log
62
+ messages created by the returned instance.
63
+
64
+ ```ruby
65
+ instance = JsonLogger.factory(:progname => 'foo', :presets => {:a => 'b', :c => 'd'})
66
+ instance.error('hello')
67
+ ```
68
+
69
+ Output:
70
+ ```json
71
+ {
72
+ "time":"2013-08-28T20:52:48.321Z",
73
+ "progname":"foo",
74
+ "level":"ERROR",
75
+ "level_id":3,
76
+ "a":"b",
77
+ "c":"d",
78
+ "msg":"hello",
79
+ }
80
+ ```
81
+
82
+ ## Copyright
83
+
84
+ Copyright (c) 2013 Alex Dean. See LICENSE.txt for
85
+ further details.
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "json_logger"
18
+ gem.homepage = "http://github.com/alexdean/json_logger"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Simple structured logging.}
21
+ gem.description = %Q{A subclass of Ruby's standard `Logger` which makes it easy to write structured data to your log files.}
22
+ gem.email = "alex@crackpot.org"
23
+ gem.authors = ["Alex Dean"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "json_logger #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,56 @@
1
+ require 'logger'
2
+ require 'time'
3
+ require 'json'
4
+
5
+ class JsonLogger < Logger
6
+
7
+ @@factory_defaults = {}
8
+ def self.factory_defaults=(options)
9
+ @@factory_defaults = options
10
+ end
11
+
12
+ # return a new JsonLogger
13
+ # - file is set in settings
14
+ # - progname and level can be set by options. (default level is in settings)
15
+ def self.factory(options={})
16
+ device = options[:device] || @@factory_defaults[:device]
17
+ inst = new(device)
18
+
19
+ inst.level = options[:level] || @@factory_defaults[:level] || Logger::WARN
20
+ inst.progname = options[:progname] || @@factory_defaults[:progname]
21
+ inst.presets = options[:presets]
22
+ inst
23
+ end
24
+
25
+ attr_accessor :presets
26
+
27
+ def initialize(*args)
28
+ super
29
+
30
+ self.formatter = proc { |severity, datetime, progname, raw|
31
+ out = {
32
+ 'time' => datetime.utc.iso8601(3),
33
+ 'level' => severity,
34
+ 'level_id' => @levels[severity]
35
+ }
36
+
37
+ if self.presets
38
+ out.merge!(self.presets)
39
+ end
40
+
41
+ if raw.respond_to?(:to_hash)
42
+ #TODO: raise error if raw has a key which overrides time, level, or level_id?
43
+ out.merge!(raw.to_hash)
44
+ elsif raw.respond_to?(:to_s)
45
+ out['msg'] = raw.to_s
46
+ end
47
+
48
+ out['progname'] = progname if progname
49
+
50
+ out.to_json + "\n"
51
+ }
52
+
53
+ @levels = {}
54
+ Logger::SEV_LABEL.each_with_index {|label,idx| @levels[label] = idx}
55
+ end
56
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "JsonLogger" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'json_logger'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alex Dean
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.8.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.8.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rdoc
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '3.12'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '3.12'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.0.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.0.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: jeweler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.8.4
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.8.4
78
+ description: A subclass of Ruby's standard `Logger` which makes it easy to write structured
79
+ data to your log files.
80
+ email: alex@crackpot.org
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files:
84
+ - LICENSE.txt
85
+ - README.md
86
+ files:
87
+ - .document
88
+ - .rspec
89
+ - .ruby-gemset
90
+ - .ruby-version
91
+ - Gemfile
92
+ - Gemfile.lock
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - VERSION
97
+ - lib/json_logger.rb
98
+ - spec/json_logger_spec.rb
99
+ - spec/spec_helper.rb
100
+ homepage: http://github.com/alexdean/json_logger
101
+ licenses:
102
+ - MIT
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ segments:
114
+ - 0
115
+ hash: -2689223519216697720
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ! '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 1.8.24
125
+ signing_key:
126
+ specification_version: 3
127
+ summary: Simple structured logging.
128
+ test_files: []