logging-json 0.1.0

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: eb741dd5e148683c7d61c6745102605c951ef49a
4
+ data.tar.gz: fb323641ef0b2661e0676042117ee7cf212b2048
5
+ SHA512:
6
+ metadata.gz: a515ed8b09cd1abdca37b6802820c25536633419888036c65e3e7bed4b5532a8368b2a1ee8e3aa29b3392ac9cf17da3a0466157305fa2b34438385ecfc457a7d
7
+ data.tar.gz: 374b7988333bee4d8baa70d31d798a228f45d9efaec976fbf6622cd89b5538c2e8e87a3fc7ee54b524cf9abbc36b84b7d596d0985b632b85130822b4b78b7767
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+ module Logging
3
+ module Json
4
+ VERSION = '0.1.0'
5
+
6
+ def self.version
7
+ VERSION
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+ require 'logging'
3
+ require_relative '../json/version'
4
+
5
+ module Logging
6
+ module Layouts
7
+ class Json < ::Logging::Layouts::Parseable
8
+ def initialize(opts = {})
9
+ opts[:style] = 'json'
10
+ super(opts)
11
+ end
12
+
13
+ def format(event)
14
+ result = {
15
+ 'logger' => event.logger,
16
+ 'timestamp' => iso8601_format(event.time),
17
+ 'level' => ::Logging::LNAMES[event.level]
18
+ }
19
+ result.merge!(log_hash(event))
20
+ MultiJson.encode(result) << "\n"
21
+ end
22
+
23
+ private
24
+
25
+ def log_hash(event)
26
+ formatted = recursive_format(event.data)
27
+ formatted.is_a?(Hash) ? formatted : {'message' => formatted}
28
+ end
29
+
30
+ def recursive_format(obj)
31
+ case obj
32
+ when Hash
33
+ format_hash(obj)
34
+ when Exception
35
+ format_exception(obj)
36
+ when Time
37
+ iso8601_format(obj)
38
+ else
39
+ format_obj(obj)
40
+ end
41
+ end
42
+
43
+ def format_hash(obj)
44
+ obj.each_with_object({}) do |(key, value), result|
45
+ result[key] = recursive_format(value)
46
+ end
47
+ end
48
+
49
+ def format_exception(exception)
50
+ result = {
51
+ 'exception' => exception.class.name,
52
+ 'message' => format_obj(exception.message)
53
+ }
54
+ result['cause'] = format_exception(exception.cause) if exception.cause
55
+ result['backtrace'] = exception.backtrace if @backtrace && exception.backtrace
56
+ result
57
+ end
58
+
59
+ # original `create_format_method` overrides `format` method with meta-programming
60
+ def create_format_method
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+ require_relative '../json/version'
3
+
4
+ module Logging
5
+ module Plugins
6
+ module Json
7
+ # This method will be called by the Logging framework when it first
8
+ # initializes. Here we require the json layout code.
9
+ def initialize_json
10
+ require_relative '../layouts/json'
11
+ end
12
+
13
+ module_function :initialize_json
14
+ end
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logging-json
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michał Knapik
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: logging
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.13'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.13'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-doc
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.43.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.43.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 1.7.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 1.7.0
111
+ description: Allows to pass objects as messages and format them as JSON
112
+ email:
113
+ - michal.knapik@u2i.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - lib/logging/json/version.rb
119
+ - lib/logging/layouts/json.rb
120
+ - lib/logging/plugins/json.rb
121
+ homepage: https://github.com/mknapik/logging-json
122
+ licenses: []
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '2.3'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.5.1
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: JSON formatter for `logging` gem
144
+ test_files: []