logz 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +35 -0
- data/LICENSE +20 -0
- data/README.md +137 -0
- data/Rakefile +6 -0
- data/lib/logz/multi_io.rb +21 -0
- data/lib/logz/multi_logger.rb +115 -0
- data/lib/logz/version.rb +3 -0
- data/lib/logz.rb +36 -0
- data/logz-1.1.0.gem +0 -0
- data/logz.gemspec +27 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7398c68219adac50a2a7b95f4adb156b62447465ff8d9c01a02491d4c1cc940d
|
4
|
+
data.tar.gz: 6f88d5bb98b1b97280332194c85f2e25fbe9cdf393da4216822dd4c3de453a69
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6ad4d67f314dd164a3971b396d62fe793169a67152ff41282ab1f35f17dbf132b18edad676cd68a03db41c108ed017ca9062e5b6a4ff6d39c7d6628659cfd72d
|
7
|
+
data.tar.gz: 3ebfad24973360068c317b9178343fca0ee43213337ffbf2cf305ac29e11bf58fadeaec5cf9e959c61f332b7befe0e251cacec2e073e0bae99c295d27781b6e8
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
logz (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.3)
|
10
|
+
rake (10.5.0)
|
11
|
+
rspec (3.8.0)
|
12
|
+
rspec-core (~> 3.8.0)
|
13
|
+
rspec-expectations (~> 3.8.0)
|
14
|
+
rspec-mocks (~> 3.8.0)
|
15
|
+
rspec-core (3.8.0)
|
16
|
+
rspec-support (~> 3.8.0)
|
17
|
+
rspec-expectations (3.8.2)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.8.0)
|
20
|
+
rspec-mocks (3.8.0)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.8.0)
|
23
|
+
rspec-support (3.8.0)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
bundler (~> 1.16)
|
30
|
+
logz!
|
31
|
+
rake (~> 10.0)
|
32
|
+
rspec (~> 3.0)
|
33
|
+
|
34
|
+
BUNDLED WITH
|
35
|
+
1.16.2
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) Ivan Tumanov
|
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,137 @@
|
|
1
|
+
# Logz
|
2
|
+
|
3
|
+
## Key features:
|
4
|
+
- Manage all your log files
|
5
|
+
- Easy and lightweight, no other gems required
|
6
|
+
- Write in two streams: STDOUT and log file when you need it
|
7
|
+
- Set level of all logs just in single method
|
8
|
+
- Add and remove
|
9
|
+
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```
|
16
|
+
gem 'logz'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install using rubygems:
|
24
|
+
|
25
|
+
$ gem install logz
|
26
|
+
|
27
|
+
### Basic usage
|
28
|
+
|
29
|
+
```
|
30
|
+
require 'logz'
|
31
|
+
|
32
|
+
logz = Logz.new
|
33
|
+
|
34
|
+
You can use this logger just as regular Logger class in Ruby:
|
35
|
+
|
36
|
+
# default logger outputs to STDOUT
|
37
|
+
logz.debug 'Foo'
|
38
|
+
logz.info 'Bar'
|
39
|
+
logz.warn 'Warning'
|
40
|
+
logz.error 'Error'
|
41
|
+
```
|
42
|
+
|
43
|
+
## Add logger
|
44
|
+
|
45
|
+
```
|
46
|
+
logz.add 'server'
|
47
|
+
# same as:
|
48
|
+
logz.add 'server', 'log/server', to_stdout: true, to_file: true
|
49
|
+
# Usage:
|
50
|
+
logz.server.info 'test'
|
51
|
+
```
|
52
|
+
|
53
|
+
This new logger will be created in the default folder, with the same name (server.log). By default, it writes both to STDOUT and log file.
|
54
|
+
|
55
|
+
## Select STDOUT or file logger
|
56
|
+
|
57
|
+
```
|
58
|
+
logz.add 'client', to_stdout: true, to_file: false
|
59
|
+
logz.client.info 'test client' # writes to STDOUT only
|
60
|
+
```
|
61
|
+
|
62
|
+
```
|
63
|
+
logz.add 'server', to_stdout: false, to_file: true
|
64
|
+
logz.server.info 'test server' # writes to log file only
|
65
|
+
```
|
66
|
+
|
67
|
+
|
68
|
+
### Add multiple loggers
|
69
|
+
|
70
|
+
```
|
71
|
+
logz.add ['server', 'client']
|
72
|
+
logz.server.warn "Foo"
|
73
|
+
logz.client.info "Bar"
|
74
|
+
# or
|
75
|
+
logz[:server].warn "Foo"
|
76
|
+
logz[:client].info "Bar"
|
77
|
+
```
|
78
|
+
|
79
|
+
### Iterate as using array
|
80
|
+
|
81
|
+
```
|
82
|
+
logz.each do |logger|
|
83
|
+
logger.level = Logger::WARN
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
87
|
+
Or do the same:
|
88
|
+
```
|
89
|
+
logz.global_level = Logger::WARN
|
90
|
+
```
|
91
|
+
|
92
|
+
### Configuration
|
93
|
+
|
94
|
+
Config file is optional. Default params:
|
95
|
+
|
96
|
+
```
|
97
|
+
Logz.configuration do |config|
|
98
|
+
# Set to nil to turn off default logger
|
99
|
+
config.default = 'stdout'
|
100
|
+
|
101
|
+
# Set loggers
|
102
|
+
config.loggers = [] # example: %w(server client important)
|
103
|
+
|
104
|
+
# Write to STDOUT by default (may be disabled on production)
|
105
|
+
config.output_to_stdout = true
|
106
|
+
|
107
|
+
# Write to log file by default (may be disabled on development)
|
108
|
+
config.output_to_file = true
|
109
|
+
|
110
|
+
# Log file default extension
|
111
|
+
config.extension = 'log'
|
112
|
+
|
113
|
+
# Default folder is ./log. You may also specify absolute path: '/var/log'
|
114
|
+
config.folder = 'log'
|
115
|
+
|
116
|
+
# Suffix for log filename
|
117
|
+
config.suffix = ''
|
118
|
+
|
119
|
+
# Prefix for log filename
|
120
|
+
config.prefix = ''
|
121
|
+
end
|
122
|
+
```
|
123
|
+
|
124
|
+
### Extra tips:
|
125
|
+
|
126
|
+
Set up custom formatter for all logs:
|
127
|
+
```
|
128
|
+
logger.each do |l|
|
129
|
+
l.formatter = proc do |severity, datetime, progname, msg|
|
130
|
+
"[#{datetime}] [#{severity}] #{msg}\n"
|
131
|
+
end
|
132
|
+
end
|
133
|
+
```
|
134
|
+
|
135
|
+
## Contributing
|
136
|
+
|
137
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/vizakenjack/logz. This project is intended to be a safe, welcoming space for collaboration.
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module Logz
|
2
|
+
class MultiIO
|
3
|
+
attr_reader :targets
|
4
|
+
|
5
|
+
def initialize(*targets)
|
6
|
+
@targets = targets
|
7
|
+
end
|
8
|
+
|
9
|
+
def write(*args)
|
10
|
+
targets.each {|t| t.write(*args); t.flush }
|
11
|
+
end
|
12
|
+
|
13
|
+
def flush
|
14
|
+
targets.each(&:flush)
|
15
|
+
end
|
16
|
+
|
17
|
+
def close
|
18
|
+
targets.each(&:close)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
module Logz
|
2
|
+
class MultiLogger
|
3
|
+
include Enumerable
|
4
|
+
attr_accessor :folder, :loggers
|
5
|
+
|
6
|
+
def initialize(folder = nil)
|
7
|
+
@folder = set_folder(folder)
|
8
|
+
@loggers = {}
|
9
|
+
|
10
|
+
FileUtils.mkdir_p(@folder) unless File.directory?(@folder)
|
11
|
+
|
12
|
+
add(Logz.configuration.default)
|
13
|
+
Logz.configuration.loggers.each { |logger_name| add(logger_name) } if Logz.configuration.loggers.any?
|
14
|
+
end
|
15
|
+
|
16
|
+
def add(name, path = '', to_stdout: nil, to_file: nil)
|
17
|
+
to_stdout = Logz.configuration.output_to_stdout if to_stdout == nil
|
18
|
+
to_file = Logz.configuration.output_to_file if to_file == nil
|
19
|
+
|
20
|
+
if !name || name.blank?
|
21
|
+
return false
|
22
|
+
elsif name == STDOUT || name == 'stdout'
|
23
|
+
name = 'stdout'
|
24
|
+
to_stdout = true
|
25
|
+
to_file = false
|
26
|
+
end
|
27
|
+
|
28
|
+
if name.is_a?(Array)
|
29
|
+
name.each { |n| add(n, path, to_stdout: to_stdout, to_file: to_file) }
|
30
|
+
else
|
31
|
+
log_path = set_log_path(path, name)
|
32
|
+
output_stream = set_output_stream(log_path, to_stdout, to_file)
|
33
|
+
|
34
|
+
@loggers[name.to_sym] = Logger.new(output_stream)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def <<(name, path = '')
|
39
|
+
add(name, path)
|
40
|
+
end
|
41
|
+
|
42
|
+
def remove(name)
|
43
|
+
@loggers[name.to_sym].close
|
44
|
+
@loggers.delete(name.to_sym)
|
45
|
+
end
|
46
|
+
alias_method :delete, :remove
|
47
|
+
|
48
|
+
def each
|
49
|
+
loggers.each do |name, logger|
|
50
|
+
yield(logger)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def [](name)
|
55
|
+
loggers[name.to_sym]
|
56
|
+
end
|
57
|
+
|
58
|
+
def global_level=(level)
|
59
|
+
loggers.each { |name, logger| logger.level = level }
|
60
|
+
end
|
61
|
+
|
62
|
+
def default_logger
|
63
|
+
loggers[Logz.configuration.default&.to_sym]
|
64
|
+
end
|
65
|
+
|
66
|
+
def method_missing(m, *args, &block)
|
67
|
+
if loggers.has_key?(m)
|
68
|
+
if args.empty?
|
69
|
+
loggers[m]
|
70
|
+
else
|
71
|
+
puts "Invalid method for logger '#{m}': #{args.join(', ')}"
|
72
|
+
end
|
73
|
+
elsif default_logger.respond_to?(m)
|
74
|
+
default_logger.send(m, *args)
|
75
|
+
else
|
76
|
+
puts "Logger '#{m}' not found. Current loggers: #{loggers.keys.join(', ')}"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
def set_folder(folder)
|
83
|
+
folder ||= Logz.configuration.folder
|
84
|
+
|
85
|
+
if !folder || folder.empty? || folder == '.' || folder == './'
|
86
|
+
Dir.pwd
|
87
|
+
elsif folder.start_with?('/')
|
88
|
+
folder
|
89
|
+
else
|
90
|
+
File.join(Dir.pwd, folder)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def set_log_path(path, name)
|
95
|
+
file_extension = Logz.configuration.extension.to_s.strip.empty? ? '' : ".#{Logz.configuration.extension}"
|
96
|
+
log_folder_path = path.start_with?('/') ? path : File.join(folder, path)
|
97
|
+
log_file_path = "/#{Logz.configuration.prefix}#{name}#{Logz.configuration.suffix}#{file_extension}"
|
98
|
+
|
99
|
+
File.join(log_folder_path, log_file_path)
|
100
|
+
end
|
101
|
+
|
102
|
+
def set_output_stream(log_path, to_stdout, to_file)
|
103
|
+
if to_stdout && to_file
|
104
|
+
MultiIO.new(STDOUT, File.open(log_path, 'a+'))
|
105
|
+
elsif to_stdout
|
106
|
+
STDOUT
|
107
|
+
elsif to_file
|
108
|
+
File.open(log_path, 'a+')
|
109
|
+
else
|
110
|
+
'/dev/null'
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
end
|
data/lib/logz/version.rb
ADDED
data/lib/logz.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'logger'
|
2
|
+
require 'logz/version'
|
3
|
+
require 'logz/multi_io'
|
4
|
+
require 'logz/multi_logger'
|
5
|
+
|
6
|
+
module Logz
|
7
|
+
class Configuration
|
8
|
+
attr_accessor :folder, :output_to_stdout, :output_to_file,
|
9
|
+
:default, :suffix, :prefix, :loggers, :extension
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@output_to_stdout = true
|
13
|
+
@output_to_file = true
|
14
|
+
@default = 'stdout'
|
15
|
+
@folder = 'log'
|
16
|
+
@extension = 'log'
|
17
|
+
@loggers = []
|
18
|
+
@suffix = ''
|
19
|
+
@prefix = ''
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class << self
|
24
|
+
attr_accessor :configuration
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.new(*params)
|
28
|
+
configure
|
29
|
+
MultiLogger.new(*params)
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.configure
|
33
|
+
self.configuration ||= Configuration.new
|
34
|
+
yield(configuration) if block_given?
|
35
|
+
end
|
36
|
+
end
|
data/logz-1.1.0.gem
ADDED
Binary file
|
data/logz.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "logz/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "logz"
|
7
|
+
spec.version = Logz::VERSION
|
8
|
+
spec.authors = ["Ivan Tumanov"]
|
9
|
+
spec.email = ["vizakenjack@gmail.com"]
|
10
|
+
spec.licenses = ['MIT']
|
11
|
+
spec.summary = %q{Simple and lightweight logger tool}
|
12
|
+
spec.description = %q{Output to STDOUT and log file at the same time. Support for multiple log files.}
|
13
|
+
spec.homepage = "https://github.com/vizakenjack/logz"
|
14
|
+
|
15
|
+
# Specify which files should be added to the gem when it is released.
|
16
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
17
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
18
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: logz
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.2.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ivan Tumanov
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-06-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: Output to STDOUT and log file at the same time. Support for multiple
|
56
|
+
log files.
|
57
|
+
email:
|
58
|
+
- vizakenjack@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- Gemfile
|
66
|
+
- Gemfile.lock
|
67
|
+
- LICENSE
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- lib/logz.rb
|
71
|
+
- lib/logz/multi_io.rb
|
72
|
+
- lib/logz/multi_logger.rb
|
73
|
+
- lib/logz/version.rb
|
74
|
+
- logz-1.1.0.gem
|
75
|
+
- logz.gemspec
|
76
|
+
homepage: https://github.com/vizakenjack/logz
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubygems_version: 3.0.3
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: Simple and lightweight logger tool
|
99
|
+
test_files: []
|