simple-logger 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +20 -0
- data/README.textile +0 -0
- data/Rakefile +0 -0
- data/lib/simple-logger.rb +3 -0
- data/lib/simple-logger/logger.rb +68 -0
- data/lib/simple-logger/plain.rb +200 -0
- data/simple-logger.gemspec +25 -0
- data/spec/simple-logger/logger_spec.rb +14 -0
- data/spec/simple-logger/plain_spec.rb +1 -0
- metadata +71 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Jakub Šťastný aka Botanicus
|
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.textile
ADDED
File without changes
|
data/Rakefile
ADDED
File without changes
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require_relative "plain"
|
4
|
+
|
5
|
+
module SimpleLogger
|
6
|
+
class Logger < Plain
|
7
|
+
@@colors = {
|
8
|
+
fatal: :red,
|
9
|
+
error: :red,
|
10
|
+
warn: :yellow,
|
11
|
+
info: :white,
|
12
|
+
debug: :cyan,
|
13
|
+
custom: :magenta
|
14
|
+
}
|
15
|
+
|
16
|
+
@@color_values = {
|
17
|
+
black: 30,
|
18
|
+
red: 31,
|
19
|
+
green: 32,
|
20
|
+
yellow: 33,
|
21
|
+
blue: 34,
|
22
|
+
magenta: 35,
|
23
|
+
cyan: 36,
|
24
|
+
white: 37
|
25
|
+
}
|
26
|
+
|
27
|
+
# Generate the logging methods for SimpleLogger.logger for each log level.
|
28
|
+
self::Levels.each_pair do |name, number|
|
29
|
+
color = @@color_values[@@colors[name]]
|
30
|
+
class_eval <<-RUBY, __FILE__, __LINE__
|
31
|
+
|
32
|
+
# Appends a message to the log if the log level is at least as high as
|
33
|
+
# the log level of the logger.
|
34
|
+
#
|
35
|
+
# ==== Parameters
|
36
|
+
# string<String>:: The message to be logged. Defaults to nil.
|
37
|
+
#
|
38
|
+
# ==== Returns
|
39
|
+
# self:: The logger object for chaining.
|
40
|
+
def #{name}(message = nil)
|
41
|
+
self << "\033[0;\#{#{color}}m%s\033[0m" % message
|
42
|
+
self
|
43
|
+
end
|
44
|
+
|
45
|
+
# Appends a message to the log if the log level is at least as high as
|
46
|
+
# the log level of the logger. The bang! version of the method also auto
|
47
|
+
# flushes the log buffer to disk.
|
48
|
+
#
|
49
|
+
# ==== Parameters
|
50
|
+
# string<String>:: The message to be logged. Defaults to nil.
|
51
|
+
#
|
52
|
+
# ==== Returns
|
53
|
+
# self:: The logger object for chaining.
|
54
|
+
def #{name}!(message = nil)
|
55
|
+
self << "\033[0;\#{#{color}}m%s\033[0m" % message
|
56
|
+
flush if #{number} >= level
|
57
|
+
self
|
58
|
+
end
|
59
|
+
|
60
|
+
# ==== Returns
|
61
|
+
# Boolean:: True if this level will be logged by this logger.
|
62
|
+
def #{name}?
|
63
|
+
#{number} >= level
|
64
|
+
end
|
65
|
+
RUBY
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,200 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "time" # httpdate
|
4
|
+
|
5
|
+
# ==== Public SimpleLogger Logger API
|
6
|
+
#
|
7
|
+
# To replace an existing logger with a new one:
|
8
|
+
# SimpleLogger::Plain.set_log(log{String, IO},level{Symbol, String})
|
9
|
+
#
|
10
|
+
# Available logging levels are
|
11
|
+
# SimpleLogger::Plain::{ Fatal, Error, Warn, Info, Debug }
|
12
|
+
#
|
13
|
+
# Logging via:
|
14
|
+
# SimpleLogger.logger.fatal(message<String>,&block)
|
15
|
+
# SimpleLogger.logger.error(message<String>,&block)
|
16
|
+
# SimpleLogger.logger.warn(message<String>,&block)
|
17
|
+
# SimpleLogger.logger.info(message<String>,&block)
|
18
|
+
# SimpleLogger.logger.debug(message<String>,&block)
|
19
|
+
#
|
20
|
+
# Logging with autoflush:
|
21
|
+
# SimpleLogger.logger.fatal!(message<String>,&block)
|
22
|
+
# SimpleLogger.logger.error!(message<String>,&block)
|
23
|
+
# SimpleLogger.logger.warn!(message<String>,&block)
|
24
|
+
# SimpleLogger.logger.info!(message<String>,&block)
|
25
|
+
# SimpleLogger.logger.debug!(message<String>,&block)
|
26
|
+
#
|
27
|
+
# Flush the buffer to
|
28
|
+
# SimpleLogger.logger.flush
|
29
|
+
#
|
30
|
+
# Remove the current log object
|
31
|
+
# SimpleLogger.logger.close
|
32
|
+
#
|
33
|
+
# ==== Private SimpleLogger Logger API
|
34
|
+
#
|
35
|
+
# To initialize the logger you create a new object, proxies to set_log.
|
36
|
+
# SimpleLogger::Plain.new(log{String, IO},level{Symbol, String})
|
37
|
+
module SimpleLogger
|
38
|
+
|
39
|
+
class << self
|
40
|
+
attr_accessor :logger
|
41
|
+
end
|
42
|
+
|
43
|
+
class Plain
|
44
|
+
|
45
|
+
attr_accessor :level
|
46
|
+
attr_accessor :delimiter
|
47
|
+
attr_accessor :auto_flush
|
48
|
+
attr_reader :buffer
|
49
|
+
attr_reader :log
|
50
|
+
attr_reader :init_args
|
51
|
+
|
52
|
+
# ==== Notes
|
53
|
+
# Ruby (standard) logger levels:
|
54
|
+
# :fatal:: An unhandleable error that results in a program crash
|
55
|
+
# :error:: A handleable error condition
|
56
|
+
# :warn:: A warning
|
57
|
+
# :info:: generic (useful) information about system operation
|
58
|
+
# :debug:: low-level information for developers
|
59
|
+
Levels =
|
60
|
+
{
|
61
|
+
:fatal => 7,
|
62
|
+
:error => 6,
|
63
|
+
:warn => 4,
|
64
|
+
:info => 3,
|
65
|
+
:debug => 0
|
66
|
+
}
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
# Readies a log for writing.
|
71
|
+
#
|
72
|
+
# ==== Parameters
|
73
|
+
# log<IO, String>:: Either an IO object or a name of a logfile.
|
74
|
+
def initialize_log(log)
|
75
|
+
close if @log # be sure that we don't leave open files laying around.
|
76
|
+
|
77
|
+
if log.respond_to?(:write)
|
78
|
+
@log = log
|
79
|
+
elsif File.exist?(log)
|
80
|
+
@log = open(log, (File::WRONLY | File::APPEND))
|
81
|
+
@log.sync = true
|
82
|
+
else
|
83
|
+
FileUtils.mkdir_p(File.dirname(log)) unless File.directory?(File.dirname(log))
|
84
|
+
@log = open(log, (File::WRONLY | File::APPEND | File::CREAT))
|
85
|
+
@log.sync = true
|
86
|
+
@log.write("#{Time.now.httpdate} #{delimiter} info #{delimiter} Logfile created\n")
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
public
|
91
|
+
|
92
|
+
# To initialize the logger you create a new object, proxies to set_log.
|
93
|
+
#
|
94
|
+
# ==== Parameters
|
95
|
+
# *args:: Arguments to create the log from. See set_logs for specifics.
|
96
|
+
def initialize(*args)
|
97
|
+
@init_args = args
|
98
|
+
set_log(*args)
|
99
|
+
end
|
100
|
+
|
101
|
+
# Replaces an existing logger with a new one.
|
102
|
+
#
|
103
|
+
# ==== Parameters
|
104
|
+
# log<IO, String>:: Either an IO object or a name of a logfile.
|
105
|
+
# log_level<~to_sym>::
|
106
|
+
# The log level from, e.g. :fatal or :info. Defaults to :error in the
|
107
|
+
# production environment and :debug otherwise.
|
108
|
+
# delimiter<String>::
|
109
|
+
# Delimiter to use between message sections. Defaults to " ~ ".
|
110
|
+
# auto_flush<Boolean>::
|
111
|
+
# Whether the log should automatically flush after new messages are
|
112
|
+
# added. Defaults to false.
|
113
|
+
def set_log(log, log_level = nil, delimiter = " ~ ", auto_flush = false)
|
114
|
+
if log_level && Levels[log_level.to_sym]
|
115
|
+
@level = Levels[log_level.to_sym]
|
116
|
+
else
|
117
|
+
@level = Levels[:debug]
|
118
|
+
end
|
119
|
+
@buffer = []
|
120
|
+
@delimiter = delimiter
|
121
|
+
@auto_flush = auto_flush
|
122
|
+
|
123
|
+
initialize_log(log)
|
124
|
+
end
|
125
|
+
|
126
|
+
# Flush the entire buffer to the log object.
|
127
|
+
def flush
|
128
|
+
return unless @buffer.size > 0
|
129
|
+
@log.write(@buffer.slice!(0..-1).join)
|
130
|
+
end
|
131
|
+
|
132
|
+
# Close and remove the current log object.
|
133
|
+
def close
|
134
|
+
flush
|
135
|
+
@log.close if @log.respond_to?(:close) && !@log.tty?
|
136
|
+
@log = nil
|
137
|
+
end
|
138
|
+
|
139
|
+
# Appends a message to the log. The methods yield to an optional block and
|
140
|
+
# the output of this block will be appended to the message.
|
141
|
+
#
|
142
|
+
# ==== Parameters
|
143
|
+
# string<String>:: The message to be logged. Defaults to nil.
|
144
|
+
#
|
145
|
+
# ==== Returns
|
146
|
+
# String:: The resulting message added to the log file.
|
147
|
+
def <<(string = nil)
|
148
|
+
message = ""
|
149
|
+
message << delimiter
|
150
|
+
message << string if string
|
151
|
+
message << "\n" unless message[-1] == ?\n
|
152
|
+
@buffer << message
|
153
|
+
flush if @auto_flush
|
154
|
+
|
155
|
+
message
|
156
|
+
end
|
157
|
+
alias :push :<<
|
158
|
+
|
159
|
+
# Generate the logging methods for SimpleLogger.logger for each log level.
|
160
|
+
Levels.each_pair do |name, number|
|
161
|
+
class_eval <<-LEVELMETHODS, __FILE__, __LINE__
|
162
|
+
|
163
|
+
# Appends a message to the log if the log level is at least as high as
|
164
|
+
# the log level of the logger.
|
165
|
+
#
|
166
|
+
# ==== Parameters
|
167
|
+
# string<String>:: The message to be logged. Defaults to nil.
|
168
|
+
#
|
169
|
+
# ==== Returns
|
170
|
+
# self:: The logger object for chaining.
|
171
|
+
def #{name}(message = nil)
|
172
|
+
self << message if #{number} >= level
|
173
|
+
self
|
174
|
+
end
|
175
|
+
|
176
|
+
# Appends a message to the log if the log level is at least as high as
|
177
|
+
# the log level of the logger. The bang! version of the method also auto
|
178
|
+
# flushes the log buffer to disk.
|
179
|
+
#
|
180
|
+
# ==== Parameters
|
181
|
+
# string<String>:: The message to be logged. Defaults to nil.
|
182
|
+
#
|
183
|
+
# ==== Returns
|
184
|
+
# self:: The logger object for chaining.
|
185
|
+
def #{name}!(message = nil)
|
186
|
+
self << message if #{number} >= level
|
187
|
+
flush if #{number} >= level
|
188
|
+
self
|
189
|
+
end
|
190
|
+
|
191
|
+
# ==== Returns
|
192
|
+
# Boolean:: True if this level will be logged by this logger.
|
193
|
+
def #{name}?
|
194
|
+
#{number} >= level
|
195
|
+
end
|
196
|
+
LEVELMETHODS
|
197
|
+
end
|
198
|
+
|
199
|
+
end
|
200
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
#!/usr/bin/env gem build
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
# NOTE: we can't use require_relative because when we run gem build, it use eval for executing this file
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "simple-logger"
|
7
|
+
s.version = "0.0.1"
|
8
|
+
s.authors = ["Jakub Šťastný aka Botanicus"]
|
9
|
+
s.homepage = "http://github.com/botanicus/simple-logger"
|
10
|
+
s.summary = "Smarter clone of Extlib logger"
|
11
|
+
s.description = "" # TODO: long description
|
12
|
+
s.cert_chain = nil
|
13
|
+
s.email = ["knava.bestvinensis", "gmail.com"].join("@")
|
14
|
+
s.has_rdoc = true
|
15
|
+
|
16
|
+
# files
|
17
|
+
s.files = Dir.glob("{lib,spec}/**/*") + %w[LICENSE Rakefile README.textile simple-logger.gemspec]
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
# Ruby version
|
21
|
+
s.required_ruby_version = ::Gem::Requirement.new("~> 1.9")
|
22
|
+
|
23
|
+
# dependencies
|
24
|
+
s.add_dependency "extlib"
|
25
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "spec"
|
4
|
+
require "tempfile"
|
5
|
+
require_relative "../lib/simple-logger"
|
6
|
+
|
7
|
+
describe SimpleLogger::Logger do
|
8
|
+
before(:each) do
|
9
|
+
@stream = Tempfile.new("test.log")
|
10
|
+
@logger = SimpleLogger::Logger.new(@stream)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should have some specs"
|
14
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
# TODO: there aren't specs for the logger in extlib, I'll have to write it myself
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple-logger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- "Jakub \xC5\xA0\xC5\xA5astn\xC3\xBD aka Botanicus"
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain:
|
11
|
+
date: 2009-12-24 00:00:00 +00:00
|
12
|
+
default_executable:
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: extlib
|
16
|
+
type: :runtime
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: "0"
|
23
|
+
version:
|
24
|
+
description: ""
|
25
|
+
email: knava.bestvinensis@gmail.com
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files: []
|
31
|
+
|
32
|
+
files:
|
33
|
+
- lib/simple-logger/logger.rb
|
34
|
+
- lib/simple-logger/plain.rb
|
35
|
+
- lib/simple-logger.rb
|
36
|
+
- spec/simple-logger/logger_spec.rb
|
37
|
+
- spec/simple-logger/plain_spec.rb
|
38
|
+
- LICENSE
|
39
|
+
- Rakefile
|
40
|
+
- README.textile
|
41
|
+
- simple-logger.gemspec
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://github.com/botanicus/simple-logger
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ~>
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "1.9"
|
56
|
+
version:
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
version:
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.3.5
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: Smarter clone of Extlib logger
|
70
|
+
test_files: []
|
71
|
+
|