simplelogger 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/LICENSE +20 -0
  2. data/README.md +95 -0
  3. data/lib/simplelogger.rb +191 -0
  4. metadata +75 -0
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 [vkhatri (Virender Khatri)](http://github.com/vkhatri)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,95 @@
1
+ simplelogger
2
+ ============
3
+
4
+ simplelogger is a simple Ruby file & console message logger
5
+
6
+
7
+ * Log messages to file, console, STDOUT, STDERR, ..
8
+ * Log levels are standard in addition of few new
9
+ * Support uuencoded message logging
10
+ * Logs different message severity with different color coding
11
+
12
+
13
+ ## Install
14
+
15
+ sudo gem install simplelogger
16
+
17
+ ## Getting Started
18
+
19
+ require 'rubygems'
20
+ require 'simplelogger'
21
+
22
+ options = {:file => 'lof_file_name', options ..}
23
+ log = Simplelogger.new(options)
24
+ log.info "message"
25
+ log.warning "message"
26
+ log.error "message"
27
+ log.fatal "message"
28
+ log.debug "message"
29
+ log.notice "message"
30
+ log.success "message"
31
+ log.fail "message"
32
+ log.underscore 'underscore message'
33
+
34
+ # Print console message with color using Term::Ansicolor
35
+ puts log.color.red 'i am red'
36
+
37
+ log.stdout 'standard output'
38
+ log.stderr 'standard error output'
39
+ log._puts 'just puts'
40
+ log._print 'just print'
41
+
42
+ log.close
43
+
44
+ ## Options
45
+
46
+ :file => string
47
+ log file name
48
+
49
+ :program_name => string
50
+ program name
51
+
52
+ :enable_debug => boolean
53
+ display and log debug messages, default no logging to console & file
54
+
55
+ :enable_nocolor => boolean
56
+ disable colorful messages on console, default enabled
57
+
58
+ :enable_verbose => boolean
59
+ display messages on console (except debug), default messages are only logged to the log file
60
+
61
+ :enable_uuencode => boolean
62
+ uuencode log messages
63
+
64
+ :time_format => string
65
+ user time format, e.g. "%Y-%m-%d_%H-%M-%S", default set to iso8601
66
+
67
+ :console_style => string
68
+ 'full' -> display full log message on console
69
+ 'message' -> display log level + log message on console
70
+ default set to 'full'
71
+
72
+
73
+ ## Copyright
74
+
75
+ The MIT License (MIT)
76
+
77
+ Copyright (c) 2013 [vkhatri (Virender Khatri)](http://github.com/vkhatri)
78
+
79
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
80
+ this software and associated documentation files (the "Software"), to deal in
81
+ the Software without restriction, including without limitation the rights to
82
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
83
+ the Software, and to permit persons to whom the Software is furnished to do so,
84
+ subject to the following conditions:
85
+
86
+ The above copyright notice and this permission notice shall be included in all
87
+ copies or substantial portions of the Software.
88
+
89
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
90
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
91
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
92
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
93
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
94
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
95
+
@@ -0,0 +1,191 @@
1
+ # Author: vkhatri (Virender Khatri)
2
+ #
3
+ # Description: A Simple Ruby Logger module with colorful console logging support
4
+ #
5
+
6
+
7
+ require 'rubygems'
8
+ require 'term/ansicolor'
9
+ require 'time'
10
+ require 'etc'
11
+
12
+ class Simplelogger
13
+
14
+ attr_reader :hostname, :color, :logfile, :options, :user
15
+ attr_accessor :enable_verbose, :enable_debug, :enable_nocolor, :enable_uuencode, :console_style, :time_format, :program_name
16
+
17
+ def initialize(opts = {})
18
+ # Options:
19
+ # :file => string
20
+ # log file name
21
+ # :program_name => string
22
+ # program name
23
+ # :enable_debug => boolean
24
+ # display and log debug messages, default no logging to console & file
25
+ # :enable_nocolor => boolean
26
+ # disable colorful messages on console, default enabled
27
+ # :enable_verbose => boolean
28
+ # display messages on console (except debug), default messages are only logged to the log file
29
+ # :enable_uuencode => boolean
30
+ # uuencode log messages
31
+ # :time_format => string
32
+ # user time format, e.g. "%Y-%m-%d_%H-%M-%S", default set to iso8601
33
+ #
34
+ # :console_style => string
35
+ # 'full' -> display full log message on console
36
+ # 'message' -> display log level + log message on console
37
+ # default set to 'full'
38
+ #
39
+
40
+ raise "missing :file option" if not opts[:file]
41
+ @logfile = File.new(opts[:file], "a") ; @logfile.sync = true
42
+
43
+ @options = opts
44
+ @color = Term::ANSIColor
45
+ $stdout.sync = true
46
+ @hostname = ENV['HOSTNAME'] || 'unknown_hostname'
47
+ @user = Etc.getlogin || 'unknown_user'
48
+
49
+ @time_format = opts[:time_format]
50
+ @program_name = opts[:program_name] || 'unknown_program'
51
+ @console_style = opts[:console_style] || 'full'
52
+ @enable_verbose = opts[:enable_verbose]
53
+ @enable_debug = opts[:enable_debug]
54
+ @enable_uuencode = opts[:enable_uuencode]
55
+ @enable_nocolor = opts[:enable_nocolor]
56
+ end
57
+
58
+ def close
59
+ logfile.close if logfile.closed?
60
+ end
61
+
62
+ def current_time
63
+ if time_format
64
+ Time.new.strftime(time_format)
65
+ else
66
+ Time.now.iso8601(5)
67
+ end
68
+ end
69
+
70
+ def uuencode(string)
71
+ if enable_uuencode
72
+ [string].pack("u")
73
+ else
74
+ string
75
+ end
76
+ end
77
+
78
+ def log(level, string)
79
+ string = uuencode(string)
80
+ message = "#{current_time} #{hostname} #{user} #{program_name} #{level}: #{string}"
81
+ logfile.puts message
82
+ message = "#{level}: #{string}" if console_style == 'message'
83
+ if enable_verbose
84
+ if enable_nocolor
85
+ puts message
86
+ else
87
+ case level
88
+ when 'INFO'
89
+ puts color.green message
90
+ when 'WARNING'
91
+ puts color.yellow message
92
+ when 'ERROR'
93
+ puts color.red message
94
+ when 'FATAL'
95
+ puts color.red message
96
+ when 'NOTICE'
97
+ puts color.blue message
98
+ when 'INIT'
99
+ puts color.bold(color.blue(message))
100
+ when 'SUCCESS'
101
+ puts color.green message
102
+ when 'FAILURE'
103
+ puts color.red message
104
+ else
105
+ puts message
106
+ end
107
+ end
108
+ end
109
+ end
110
+
111
+ def info(string)
112
+ string = uuencode(string)
113
+ log('INFO',string)
114
+ end
115
+
116
+ def debug(string)
117
+ # Default debug messages are not logged, kept it out of 'log'
118
+ if enable_debug
119
+ string = uuencode(string)
120
+ message = "#{current_time} #{hostname} #{user} #{program_name} DEBUG: #{string}"
121
+ logfile.puts message
122
+ message = "DEBUG: #{string}" if console_style == 'message'
123
+ if enable_nocolor
124
+ puts messages
125
+ else
126
+ puts color.magenta message
127
+ end
128
+ end
129
+ end
130
+
131
+ def warning(string)
132
+ string = uuencode(string)
133
+ log('WARNING',string)
134
+ end
135
+
136
+ def error(string)
137
+ string = uuencode(string)
138
+ log('ERROR',string)
139
+ end
140
+
141
+ def fatal(string)
142
+ string = uuencode(string)
143
+ log('FATAL',string)
144
+ end
145
+
146
+ def notice(string)
147
+ string = uuencode(string)
148
+ log('NOTICE',string)
149
+ end
150
+
151
+ def init(string)
152
+ string = uuencode(string)
153
+ log('INIT',string)
154
+ end
155
+
156
+ def success(string)
157
+ string = uuencode(string)
158
+ log('SUCCESS',string)
159
+ end
160
+
161
+ def fail(string)
162
+ string = uuencode(string)
163
+ log('FAILURE',string)
164
+ end
165
+
166
+ def underscore(string)
167
+ string = uuencode(string)
168
+ puts color.underscore string
169
+ end
170
+
171
+ def stderr(string)
172
+ string = uuencode(string)
173
+ $stderr.write string
174
+ end
175
+
176
+ def stdout(string)
177
+ string = uuencode(string)
178
+ $stdout.write string
179
+ end
180
+
181
+ def _puts(string)
182
+ string = uuencode(string)
183
+ puts string
184
+ end
185
+
186
+ def _print(string)
187
+ string = uuencode(string)
188
+ print string
189
+ end
190
+
191
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simplelogger
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - vkhatri (Virender Khatri)
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2013-11-04 00:00:00 +05:30
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: term-ansicolor
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ version_requirements: *id001
32
+ description: Simple Ruby module to log messages to file & console
33
+ email: vir.khatri@gmail.com
34
+ executables: []
35
+
36
+ extensions: []
37
+
38
+ extra_rdoc_files: []
39
+
40
+ files:
41
+ - README.md
42
+ - LICENSE
43
+ - lib/simplelogger.rb
44
+ has_rdoc: true
45
+ homepage: https://github.com/vkhatri/simplelogger
46
+ licenses:
47
+ - MIT
48
+ post_install_message:
49
+ rdoc_options: []
50
+
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.3.6
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Ruby File/Console Message Logger
74
+ test_files: []
75
+