betterlog 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
+ SHA256:
3
+ metadata.gz: 5adb87a37d7aa07a1777d8359078978b496e47b7941abc0292e9e0c0136d89b8
4
+ data.tar.gz: 1c7318d6b0cac6dcd989623f9354cafb8455802796479a9576bd80e1d5fadbac
5
+ SHA512:
6
+ metadata.gz: bf87372fb489e12ed1683aa6cfd976bbb85521377811708a911905947beb058c68f0f84b415248cdd9180e4749294bdd58cc1669e7f04888482d34905072ebd1
7
+ data.tar.gz: 75a2bb98d9bde7c1d3088c3cc999d0eb92107a5d0fef312b920c74fd86102dad12e81745f519f90a5bc51eda78ca2f4db28efa203c291f5b9e4a87db884cf2ba
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ .*.sw[pon]
2
+ .AppleDouble
3
+ .DS_Store
4
+ .bundle
5
+ .byebug_history
6
+ .ruby-version
7
+ .rvmrc
8
+ .utilsrc
9
+ .yardoc
10
+ Gemfile.lock
11
+ coverage
12
+ errors.lst
13
+ pkg
14
+ tags
data/Gemfile ADDED
@@ -0,0 +1 @@
1
+ gem 'gem_hadar'
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright {yyyy} {name of copyright owner}
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
202
+
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # betterlog
2
+
3
+ Logging tools for betterplace structured logging
data/Rakefile ADDED
@@ -0,0 +1,30 @@
1
+ # vim: set filetype=ruby et sw=2 ts=2:
2
+
3
+ require 'gem_hadar'
4
+
5
+ GemHadar do
6
+ name 'betterlog'
7
+ author 'betterplace Developers'
8
+ email 'developers@betterplace.org'
9
+ homepage "http://github.com/betterplace/#{name}"
10
+ summary 'Structured logging support for bp'
11
+ description "This library provides structure json logging for our rails projects"
12
+ test_dir 'spec'
13
+ ignore '.*.sw[pon]', 'pkg', 'Gemfile.lock', 'coverage', '.rvmrc',
14
+ '.ruby-version', '.AppleDouble', 'tags', '.DS_Store', '.utilsrc',
15
+ '.bundle', '.byebug_history', 'errors.lst', '.yardoc'
16
+ readme 'README.md'
17
+ title "#{name.camelize}"
18
+ executables ["betterlog"]
19
+
20
+ dependency 'tins', '~>1.3', '>=1.3.5'
21
+ dependency 'rails', '>=3', '<6'
22
+ dependency 'complex_config'
23
+ dependency 'file-tail', '~>1.0'
24
+ dependency 'json', '~>2.0'
25
+ dependency 'term-ansicolor', '~>1.3'
26
+
27
+ development_dependency 'rake'
28
+ end
29
+
30
+ task :default => :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,53 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # stub: betterdocs 0.0.1 ruby lib
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "betterdocs".freeze
6
+ s.version = "0.0.1"
7
+
8
+ s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
9
+ s.require_paths = ["lib".freeze]
10
+ s.authors = ["betterplace Developers".freeze]
11
+ s.date = "2018-12-05"
12
+ s.description = "This library provides structure json logging for our rails projects".freeze
13
+ s.email = "developers@betterplace.org".freeze
14
+ s.extra_rdoc_files = ["README.md".freeze, "lib/betterlog.rb".freeze, "lib/betterlog/event.rb".freeze, "lib/betterlog/event_formatter.rb".freeze, "lib/betterlog/log.rb".freeze, "lib/betterlog/log_event_formatter.rb".freeze, "lib/betterlog/severity.rb".freeze]
15
+ s.files = ["README.md".freeze, "lib/betterlog.rb".freeze, "lib/betterlog/event.rb".freeze, "lib/betterlog/event_formatter.rb".freeze, "lib/betterlog/log.rb".freeze, "lib/betterlog/log_event_formatter.rb".freeze, "lib/betterlog/severity.rb".freeze]
16
+ s.homepage = "http://github.com/betterplace/betterdocs".freeze
17
+ s.rdoc_options = ["--title".freeze, "Betterdocs".freeze, "--main".freeze, "README.md".freeze]
18
+ s.rubygems_version = "2.7.6".freeze
19
+ s.summary = "Structured logging support for bp".freeze
20
+
21
+ if s.respond_to? :specification_version then
22
+ s.specification_version = 4
23
+
24
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
25
+ s.add_development_dependency(%q<gem_hadar>.freeze, ["~> 1.9.1"])
26
+ s.add_development_dependency(%q<rake>.freeze, [">= 0"])
27
+ s.add_runtime_dependency(%q<tins>.freeze, [">= 1.3.5", "~> 1.3"])
28
+ s.add_runtime_dependency(%q<rails>.freeze, ["< 6", ">= 3"])
29
+ s.add_runtime_dependency(%q<complex_config>.freeze, [">= 0"])
30
+ s.add_runtime_dependency(%q<file-tail>.freeze, ["~> 1.0"])
31
+ s.add_runtime_dependency(%q<json>.freeze, ["~> 2.0"])
32
+ s.add_runtime_dependency(%q<term-ansicolor>.freeze, ["~> 1.3"])
33
+ else
34
+ s.add_dependency(%q<gem_hadar>.freeze, ["~> 1.9.1"])
35
+ s.add_dependency(%q<rake>.freeze, [">= 0"])
36
+ s.add_dependency(%q<tins>.freeze, [">= 1.3.5", "~> 1.3"])
37
+ s.add_dependency(%q<rails>.freeze, ["< 6", ">= 3"])
38
+ s.add_dependency(%q<complex_config>.freeze, [">= 0"])
39
+ s.add_dependency(%q<file-tail>.freeze, ["~> 1.0"])
40
+ s.add_dependency(%q<json>.freeze, ["~> 2.0"])
41
+ s.add_dependency(%q<term-ansicolor>.freeze, ["~> 1.3"])
42
+ end
43
+ else
44
+ s.add_dependency(%q<gem_hadar>.freeze, ["~> 1.9.1"])
45
+ s.add_dependency(%q<rake>.freeze, [">= 0"])
46
+ s.add_dependency(%q<tins>.freeze, [">= 1.3.5", "~> 1.3"])
47
+ s.add_dependency(%q<rails>.freeze, ["< 6", ">= 3"])
48
+ s.add_dependency(%q<complex_config>.freeze, [">= 0"])
49
+ s.add_dependency(%q<file-tail>.freeze, ["~> 1.0"])
50
+ s.add_dependency(%q<json>.freeze, ["~> 2.0"])
51
+ s.add_dependency(%q<term-ansicolor>.freeze, ["~> 1.3"])
52
+ end
53
+ end
data/betterlog.gemspec ADDED
@@ -0,0 +1,54 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # stub: betterlog 0.1.0 ruby lib
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "betterlog".freeze
6
+ s.version = "0.1.0"
7
+
8
+ s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
9
+ s.require_paths = ["lib".freeze]
10
+ s.authors = ["betterplace Developers".freeze]
11
+ s.date = "2019-01-10"
12
+ s.description = "This library provides structure json logging for our rails projects".freeze
13
+ s.email = "developers@betterplace.org".freeze
14
+ s.executables = ["betterlog".freeze]
15
+ s.extra_rdoc_files = ["README.md".freeze, "lib/betterdocs/version.rb".freeze, "lib/betterlog.rb".freeze, "lib/betterlog/betterlog_railtie.rb".freeze, "lib/betterlog/global_metadata.rb".freeze, "lib/betterlog/log.rb".freeze, "lib/betterlog/log/event.rb".freeze, "lib/betterlog/log/event_formatter.rb".freeze, "lib/betterlog/log/severity.rb".freeze, "lib/betterlog/log_event_formatter.rb".freeze, "lib/betterlog/version.rb".freeze]
16
+ s.files = [".gitignore".freeze, "Gemfile".freeze, "LICENSE".freeze, "README.md".freeze, "Rakefile".freeze, "VERSION".freeze, "betterdocs.gemspec".freeze, "betterlog.gemspec".freeze, "bin/betterlog".freeze, "lib/betterdocs/version.rb".freeze, "lib/betterlog.rb".freeze, "lib/betterlog/betterlog_railtie.rb".freeze, "lib/betterlog/global_metadata.rb".freeze, "lib/betterlog/log.rb".freeze, "lib/betterlog/log/event.rb".freeze, "lib/betterlog/log/event_formatter.rb".freeze, "lib/betterlog/log/severity.rb".freeze, "lib/betterlog/log_event_formatter.rb".freeze, "lib/betterlog/version.rb".freeze, "log.yml".freeze]
17
+ s.homepage = "http://github.com/betterplace/betterlog".freeze
18
+ s.rdoc_options = ["--title".freeze, "Betterlog".freeze, "--main".freeze, "README.md".freeze]
19
+ s.rubygems_version = "3.0.1".freeze
20
+ s.summary = "Structured logging support for bp".freeze
21
+
22
+ if s.respond_to? :specification_version then
23
+ s.specification_version = 4
24
+
25
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
26
+ s.add_development_dependency(%q<gem_hadar>.freeze, ["~> 1.9.1"])
27
+ s.add_development_dependency(%q<rake>.freeze, [">= 0"])
28
+ s.add_runtime_dependency(%q<tins>.freeze, ["~> 1.3", ">= 1.3.5"])
29
+ s.add_runtime_dependency(%q<rails>.freeze, [">= 3", "< 6"])
30
+ s.add_runtime_dependency(%q<complex_config>.freeze, [">= 0"])
31
+ s.add_runtime_dependency(%q<file-tail>.freeze, ["~> 1.0"])
32
+ s.add_runtime_dependency(%q<json>.freeze, ["~> 2.0"])
33
+ s.add_runtime_dependency(%q<term-ansicolor>.freeze, ["~> 1.3"])
34
+ else
35
+ s.add_dependency(%q<gem_hadar>.freeze, ["~> 1.9.1"])
36
+ s.add_dependency(%q<rake>.freeze, [">= 0"])
37
+ s.add_dependency(%q<tins>.freeze, ["~> 1.3", ">= 1.3.5"])
38
+ s.add_dependency(%q<rails>.freeze, [">= 3", "< 6"])
39
+ s.add_dependency(%q<complex_config>.freeze, [">= 0"])
40
+ s.add_dependency(%q<file-tail>.freeze, ["~> 1.0"])
41
+ s.add_dependency(%q<json>.freeze, ["~> 2.0"])
42
+ s.add_dependency(%q<term-ansicolor>.freeze, ["~> 1.3"])
43
+ end
44
+ else
45
+ s.add_dependency(%q<gem_hadar>.freeze, ["~> 1.9.1"])
46
+ s.add_dependency(%q<rake>.freeze, [">= 0"])
47
+ s.add_dependency(%q<tins>.freeze, ["~> 1.3", ">= 1.3.5"])
48
+ s.add_dependency(%q<rails>.freeze, [">= 3", "< 6"])
49
+ s.add_dependency(%q<complex_config>.freeze, [">= 0"])
50
+ s.add_dependency(%q<file-tail>.freeze, ["~> 1.0"])
51
+ s.add_dependency(%q<json>.freeze, ["~> 2.0"])
52
+ s.add_dependency(%q<term-ansicolor>.freeze, ["~> 1.3"])
53
+ end
54
+ end
data/bin/betterlog ADDED
@@ -0,0 +1,240 @@
1
+ #!/usr/bin/env ruby
2
+ # vim: set ft=ruby et sw=2 ts=2:
3
+
4
+ require 'betterlog'
5
+ require 'tins/go'
6
+ require 'file-tail'
7
+ require 'complex_config/rude'
8
+ require 'zlib'
9
+
10
+ class Betterlog
11
+ def initialize(args = ARGV.dup)
12
+ STDOUT.sync = true
13
+ @args = args
14
+ @opts = Tins::GO.go 'cfhp:e:s:S:n:F:', @args, defaults: { ?c => true, ?p => ?d }
15
+ filter_severities
16
+ @opts[?h] and usage
17
+ end
18
+
19
+ def usage
20
+ puts <<~end
21
+ Usage: #{prog} [OPTIONS] [LOGFILES]
22
+
23
+ Options are
24
+
25
+ -c to enable colors during pretty printing
26
+ -f to follow the log files
27
+ -h to display this help
28
+ -p FORMAT to pretty print the log file if possible
29
+ -e EMITTER only output events from these emitters
30
+ -s MATCH only display events matching this search string
31
+ -S SEVERITY only output events with severity, e. g. -S '>=warn'
32
+ -n NUMBER rewind this many lines backwards before tailing log file
33
+ -F SHORTCUT to open the config files with SHORTCUT
34
+
35
+ FORMAT values are: #{Array(cc.log.formats?&.attribute_names) * ?,}
36
+
37
+ SEVERITY values are: #{Log::Severity.all * ?|}
38
+
39
+ Config file SHORTCUTs are: #{Array(cc.log.config_files?&.attribute_names) * ?,}
40
+
41
+ Note, that you can use multiple SHORTCUTs via "-F foo -F bar".
42
+
43
+ Examples:
44
+
45
+ - Follow rails log in long format with colors for errors or greater:
46
+
47
+ $ betterlog -f -F rails -p long -c -S ">=error"
48
+
49
+ - Follow rails AND redis logs with default format in colors
50
+ including the last 10 lines:
51
+
52
+ $ betterlog -f -F rails -F redis -pd -c -n 10
53
+
54
+ - Filter stdin from file unicorn.log with default format in color:
55
+
56
+ $ betterlog -pd -c <unicorn.log
57
+
58
+ - Filter the last 10 lines of file unicorn.log with default format
59
+ in color:
60
+
61
+ $ betterlog -c -pd -n 10 unicorn.log
62
+
63
+ - Filter the last 10 lines of file unicorn.log as JSON events:
64
+
65
+ $ betterlog -n 10 unicorn.log
66
+
67
+ end
68
+ exit(0)
69
+ end
70
+
71
+ private\
72
+ def filter_severities
73
+ @severities = Log::Severity.all
74
+ if severity = @opts[?S]
75
+ severity.each do |s|
76
+ if s =~ /\A(>=?|<=?)(.+)/
77
+ gs = Log::Severity.new($2)
78
+ @severities.select! { |x| x.send($1, gs) }
79
+ else
80
+ gs = Log::Severity.new(s)
81
+ @severities.select! { |x| x == gs }
82
+ end
83
+ end
84
+ end
85
+ end
86
+
87
+ def prog
88
+ File.basename($0)
89
+ end
90
+
91
+ def emitters
92
+ Array(@opts[?e])
93
+ end
94
+
95
+ def search_matched?(event)
96
+ case @opts[?s]
97
+ when /:\?\z/
98
+ event[$`].present?
99
+ when /:([^:]+)\z/
100
+ event[$`].full?(:include?, $1)
101
+ when String
102
+ event.to_json.include?(@opts[?s])
103
+ else
104
+ return true
105
+ end
106
+ end
107
+
108
+ def output_log_event(prefix, event)
109
+ return unless @severities.include?(event.severity)
110
+ return if emitters.full? && !emitters.include?(event.emitter)
111
+ search_matched?(event) or return
112
+ if format = @opts[?p]
113
+ puts event.format(pretty: :format, color: @opts[?c], format: format)
114
+ else
115
+ puts "#{prefix}#{event}"
116
+ end
117
+ end
118
+
119
+ def output_log_line(l, filename)
120
+ l.blank? and return
121
+ prefix =
122
+ if filename && @args.size > 1
123
+ "#{filename}: "
124
+ end
125
+ if event = Log::Event.parse(l)
126
+ filename and event[:file] = filename
127
+ output_log_event(prefix, event)
128
+ elsif l =~ /^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3})\d* (.*)/
129
+ event = Log::Event.new(
130
+ timestamp: $1,
131
+ message: Term::ANSIColor.uncolor($2),
132
+ type: 'isoprefix',
133
+ )
134
+ filename and event[:file] = filename
135
+ output_log_event(prefix, event)
136
+ else
137
+ @opts[?e] or puts "#{prefix}#{l}"
138
+ end
139
+ rescue
140
+ @opts[?e] or puts "#{prefix}#{l}"
141
+ end
142
+
143
+ def query_config_file_configuration
144
+ if @opts[?F]
145
+ if cfs = cc.log.config_files?
146
+ @opts[?F].each do |f|
147
+ @args.concat cfs[f]
148
+ end
149
+ else
150
+ fail "no config files for #{@opts[?F]} defined"
151
+ end
152
+ else
153
+ if @args.empty? and r = cc.log.config_files?&.rails?
154
+ @args.concat r
155
+ end
156
+ if @args.empty?
157
+ fail "filenames to follow needed"
158
+ end
159
+ end
160
+ @args.uniq!
161
+ end
162
+
163
+ def follow_files
164
+ group = File::Tail::Group.new
165
+ @args.each do |f|
166
+ if File.exist?(f)
167
+ group.add_filename f, @opts[?n].to_i
168
+ else
169
+ STDERR.puts "file #{f.inspect} does not exist, skip it!"
170
+ end
171
+ end
172
+ group.each_file { |f| f.max_interval = 1 }
173
+ t = Thread.new do
174
+ group.tail { |l| output_log_line(l, l.file.path) }
175
+ end
176
+ t.join
177
+ rescue Interrupt
178
+ end
179
+
180
+ def filter_argv
181
+ for fn in @args
182
+ unless File.exist?(fn)
183
+ STDERR.puts "file #{fn.inspect} does not exist, skip it!"
184
+ next
185
+ end
186
+ if fn.end_with?('.gz')
187
+ Zlib::GzipReader.open(fn) do |f|
188
+ f.extend(File::Tail)
189
+ f.each_line do |l|
190
+ output_log_line(l, fn)
191
+ end
192
+ end
193
+ else
194
+ File::Tail::Logfile.open(fn, backward: @opts[?n].to_i) do |f|
195
+ f.each_line do |l|
196
+ output_log_line(l, fn)
197
+ end
198
+ end
199
+ end
200
+ end
201
+ end
202
+
203
+ def filter_stdin
204
+ STDIN.each_line do |l|
205
+ output_log_line(l, nil)
206
+ end
207
+ end
208
+
209
+ def output_log_sources
210
+ if @args.empty?
211
+ STDERR.puts "#{prog} tracking stdin\nseverities: #{@severities * ?|}"
212
+ else
213
+ STDERR.puts "#{prog} tracking files:\n"\
214
+ "#{@args.map { |a| ' ' + a.inspect }.join(' ')}\n"\
215
+ "severities: #{@severities * ?|}\n"
216
+ end
217
+ end
218
+
219
+ def run
220
+ if @opts[?f]
221
+ query_config_file_configuration
222
+ output_log_sources
223
+ follow_files
224
+ elsif @opts[?F] && @args.empty?
225
+ query_config_file_configuration
226
+ output_log_sources
227
+ filter_argv
228
+ elsif !@args.empty?
229
+ output_log_sources
230
+ filter_argv
231
+ else
232
+ output_log_sources
233
+ filter_stdin
234
+ end
235
+ end
236
+ end
237
+
238
+ if File.basename($0) == File.basename(__FILE__)
239
+ Betterlog.new(ARGV).run
240
+ end
@@ -0,0 +1,8 @@
1
+ module Betterdocs
2
+ # Betterdocs version
3
+ VERSION = '0.0.1'
4
+ VERSION_ARRAY = VERSION.split('.').map(&:to_i) # :nodoc:
5
+ VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
+ VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
7
+ VERSION_BUILD = VERSION_ARRAY[2] # :nodoc:
8
+ end
@@ -0,0 +1,5 @@
1
+ class BetterlogRailtie < Rails::Railtie
2
+ initializer "betterlog_railtie.configure_rails_initialization" do
3
+ Rails.logger.formatter = LogEventFormatter.new
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ # To retrieve thread-global metadata that is used to enrich data that is sent
2
+ # to logging and debugging tools. In addition to holding the data
3
+ # thread-global, this will also attempt to update context of error reporting
4
+ # tools etc.
5
+
6
+ class GlobalMetadata
7
+ include Tins::SexySingleton
8
+
9
+ def data
10
+ Thread.current['BP_GLOBAL_METATDATA'] || {}
11
+ end
12
+
13
+ def add(data_hash)
14
+ data = data_hash | data
15
+ Honeybadger.context(data_hash)
16
+ end
17
+
18
+ private
19
+
20
+ def data=(value)
21
+ Thread.current['BP_GLOBAL_METATDATA'] = value
22
+ end
23
+ end