pyer-logger 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/pyer/logger.rb +202 -0
  3. metadata +73 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4894005ac53da8f7ef99404bb956084a6597b74a
4
+ data.tar.gz: a1f12c6a90d1f0b40119569ea4e644f0cacf4179
5
+ SHA512:
6
+ metadata.gz: 1e2d24fc6088ae0e165f7c5161e3f1a503e90134669dfb071974676df6d578079691a67e5ee7bd393aad16828c3245afaaff875c7daf87b89677551add4af111
7
+ data.tar.gz: ad1bdecdf44243373a05c67c52cddd8eea57060a027bd43770bdf1732ba5104b4071027c86c8d2da4cd82e8e84f4d3182a045f754b9a3213d3e4a64f96d6ef5c
@@ -0,0 +1,202 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # == Description
4
+ #
5
+ # The Logger class provides a simple logging utility.
6
+ #
7
+ # You create a Logger object (output to a file, STDOUT or STDERR).
8
+ # The messages will have varying levels (+info+, +error+, etc),
9
+ # reflecting their varying importance.
10
+ #
11
+ # The levels, and their meanings, are:
12
+ # +ERROR+:: an error condition
13
+ # +WARN +:: a warning
14
+ # +INFO +:: generic (useful) information about system operation
15
+ # +DEBUG+:: low-level information for developers
16
+ #
17
+ # So each message has a level, and the Logger itself has a level, which acts
18
+ # as a filter, so you can control the amount of information emitted from the
19
+ # logger without having to remove actual messages.
20
+ #
21
+ # == HOWTOs
22
+ #
23
+ # === How to create a logger
24
+ #
25
+ # 1. Create a logger which logs messages to STDERR/STDOUT.
26
+ # logger = Logger.new(STDOUT)
27
+ # logger = Logger.new(STDERR)
28
+ #
29
+ # 2. Create a logger for the file which has the specified name.
30
+ # logger = Logger.new('logfile.log')
31
+ #
32
+ # === How to log a message
33
+ #
34
+ # Notice the different methods (+fatal+, +error+, +warn+, +info+, +debug+)
35
+ # being used to log messages of various levels.
36
+ # Messages lower than logger.level are not emitted.
37
+ #
38
+ # 1. Message in block.
39
+ #
40
+ # logger.error { "Argument 'foo' not given." }
41
+ #
42
+ # 2. Message as a string.
43
+ #
44
+ # logger.error "Argument #{ @foo } mismatch."
45
+ #
46
+ # === How to set severity level
47
+ #
48
+ # logger.level = Logger::INFO
49
+ #
50
+ # DEBUG < INFO < WARN < ERROR
51
+ #
52
+ # === How to close a logger
53
+ #
54
+ # logger.close
55
+ #
56
+ # All my tools are in module Pyer
57
+ module Pyer
58
+ require 'stringio'
59
+
60
+ # Logger class
61
+ class Logger
62
+ # Logging severity.
63
+ module Severity
64
+ DEBUG = 0
65
+ INFO = 1
66
+ WARN = 2
67
+ ERROR = 3
68
+ # Severity label for logging. (max 5 char)
69
+ SEVERITY_LABELS = ['DEBUG', 'INFO ', 'WARN ', 'ERROR']
70
+ COLOURED_LABELS = ["\033[44mDEBUG\033[m", "\033[42mINFO \033[m", "\033[43mWARN \033[m", "\033[41mERROR\033[m"]
71
+ end
72
+ include Severity
73
+
74
+ STRING = -1
75
+
76
+ # Logging severity threshold (e.g. <tt>Logger::INFO</tt>).
77
+ attr_accessor :level
78
+
79
+ # Returns +true+ if the current severity level allows the printing of the message
80
+
81
+ def debug?
82
+ @level <= DEBUG
83
+ end
84
+
85
+ def info?
86
+ @level <= INFO
87
+ end
88
+
89
+ def warn?
90
+ @level <= WARN
91
+ end
92
+
93
+ def error?
94
+ @level <= ERROR
95
+ end
96
+
97
+ # Create an instance.
98
+ # outputs log messages on STDOUT, STDERR, a file or a StringIO
99
+ #
100
+ def initialize(logdev = nil, klass = nil)
101
+ @level = DEBUG
102
+ @logdev = $stdout
103
+ @severity_label = COLOURED_LABELS
104
+ @klass_name = ''
105
+ @klass_name = klass.name unless klass.nil?
106
+ return if logdev.nil?
107
+ if logdev == STRING
108
+ # no log device implies that messages are stored in a string
109
+ @logdev = StringIO.new
110
+ @severity_label = SEVERITY_LABELS
111
+ else
112
+ if logdev == STDOUT
113
+ @logdev = $stdout
114
+ else
115
+ if logdev == STDERR
116
+ @logdev = $stderr
117
+ else
118
+ # the default log device is a file name
119
+ @logdev = File.new(logdev.to_s, 'a')
120
+ @severity_label = SEVERITY_LABELS
121
+ end
122
+ end
123
+ end
124
+ end
125
+
126
+ def string
127
+ @logdev.class == StringIO ? @logdev.string : ''
128
+ end
129
+
130
+ # Close the logging device.
131
+ def close
132
+ @logdev.close if @logdev.class == File
133
+ end
134
+
135
+ # Log a +DEBUG+ message.
136
+ def debug(message = nil, &block)
137
+ add(DEBUG, message, &block)
138
+ end
139
+
140
+ # Log an +INFO+ message.
141
+ def info(message = nil, &block)
142
+ add(INFO, message, &block)
143
+ end
144
+
145
+ # Log a +WARN+ message.
146
+ def warn(message = nil, &block)
147
+ add(WARN, message, &block)
148
+ end
149
+
150
+ # Log an +ERROR+ message.
151
+ def error(message = nil, &block)
152
+ add(ERROR, message, &block)
153
+ end
154
+
155
+ private
156
+
157
+ # Log a message if the given severity is high enough.
158
+ #
159
+ def add(severity, message, &block)
160
+ return if @logdev.nil? || severity < @level
161
+ message = '' if message.nil?
162
+ message += block.call if block_given?
163
+ @logdev.write(Formatter.new.string(@klass_name, @severity_label[severity], message))
164
+ end
165
+ end
166
+
167
+ # Formatter class
168
+ class Formatter
169
+ FORMAT = "%s [%5d] %12s %s %s\n"
170
+ def string(klass_name, label, message = nil)
171
+ format(FORMAT, format_datetime(Time.now), $$, klass_name, label, format_message(message))
172
+ end
173
+
174
+ private
175
+
176
+ def format_datetime(time)
177
+ time.strftime('%Y-%m-%d %H:%M:%S.') << format('%06d ', time.usec)
178
+ # time.strftime("%Y-%m-%d %H:%M:%S ")
179
+ end
180
+
181
+ def format_message(msg)
182
+ return '' if msg.nil?
183
+ case msg
184
+ when ::String
185
+ msg
186
+ when ::Exception
187
+ "#{msg.message} (#{msg.class})\n" << (msg.backtrace || []).join("\n")
188
+ else
189
+ msg.inspect
190
+ end
191
+ end
192
+ end
193
+ end
194
+
195
+ # Backward-compatible alias
196
+ Logger = Pyer::Logger
197
+ STRING = Pyer::Logger::STRING
198
+
199
+ DEBUG = Pyer::Logger::DEBUG
200
+ INFO = Pyer::Logger::INFO
201
+ WARN = Pyer::Logger::WARN
202
+ ERROR = Pyer::Logger::ERROR
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pyer-logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Pierre BAZONNARD
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 5.4.2
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 5.4.2
41
+ description:
42
+ email:
43
+ - pierre.bazonnard@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/pyer/logger.rb
49
+ homepage: https://github.com/pyer/logger
50
+ licenses:
51
+ - MIT
52
+ metadata: {}
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 2.1.0
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 2.4.5
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: Logger
73
+ test_files: []