flogger 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +5 -1
- data/README.rdoc +27 -11
- data/examples/clear_logs.rb +39 -0
- data/examples/custom_output.rb +4 -4
- data/examples/default_output.rb +3 -3
- data/flogger.gemspec +5 -8
- data/lib/flogger.rb +1 -0
- data/lib/flogger/ext/file.rb +22 -0
- data/lib/flogger/log.rb +50 -14
- data/lib/flogger/version.rb +1 -1
- metadata +8 -6
data/CHANGELOG.rdoc
CHANGED
@@ -1,4 +1,8 @@
|
|
1
|
-
*0.1.
|
1
|
+
*0.1.2* August 21, 2011
|
2
|
+
* added ability to clear contents of existing log files
|
3
|
+
* changed default log file to 'logs/application.txt'
|
4
|
+
|
5
|
+
*0.1.1* August 20, 2011
|
2
6
|
* added error handling when accessing log files
|
3
7
|
* changed default log directory from 'log' to 'logs'
|
4
8
|
|
data/README.rdoc
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
= FLogger
|
2
2
|
|
3
|
-
FLogger is a really simple Ruby-based logging system that outputs to one or many log files. It was created as a learning project for myself, but also something that I could continue to develop and use with future applications.
|
3
|
+
FLogger (or File Logger) is a really simple Ruby-based logging system that outputs to one or many log files. It was created as a learning project for myself, but also something that I could continue to develop and use with future applications.
|
4
|
+
|
4
5
|
|
5
6
|
== Installing
|
6
7
|
|
@@ -8,15 +9,17 @@ FLogger can be downloaded from source or installed as a gem with the command:
|
|
8
9
|
|
9
10
|
gem install flogger
|
10
11
|
|
12
|
+
|
11
13
|
== Usage and Default
|
12
14
|
|
13
15
|
Some examples of using FLogger within a simple Ruby application are shown in the examples directory. Currently, FLogger can output to various predefined logs (and perhaps more in the future). By default, all logs are placed in the 'logs' directory within your current working directory. Currently, these are the log types available and their default location:
|
14
16
|
|
15
|
-
* *
|
16
|
-
* *
|
17
|
-
* *
|
18
|
-
* *
|
19
|
-
* *
|
17
|
+
* *debug:* Default location is logs/debug.txt
|
18
|
+
* *error:* Default location is logs/error.txt
|
19
|
+
* *notice:* Default location is logs/notice.txt
|
20
|
+
* *warning:* Default location is logs/warning.txt
|
21
|
+
* *default:* Default location is logs/application.txt
|
22
|
+
|
20
23
|
|
21
24
|
== Examples
|
22
25
|
|
@@ -28,18 +31,31 @@ More detailed examples of how to use FLogger can be found in the examples direct
|
|
28
31
|
|
29
32
|
log.debug 'This message will be stored in logs/debug.txt'
|
30
33
|
log.error 'This message will be stored in logs/error.txt'
|
31
|
-
log.puts
|
34
|
+
log.puts 'This message will be stored in logs/application.txt'
|
32
35
|
|
33
|
-
This example shows how easy it is to
|
36
|
+
This example shows how easy it is to change the location FLogger outputs log files:
|
34
37
|
|
35
38
|
log = FLogger::Log.new
|
36
39
|
|
40
|
+
# change a few log locations
|
37
41
|
log.configure do |c|
|
38
|
-
c.debug
|
39
|
-
c.error
|
42
|
+
c.debug = 'new/logs/debug_log.txt'
|
43
|
+
c.error = 'new/logs/error_log.txt'
|
40
44
|
c.default = 'new/logs/default_log.txt'
|
41
45
|
end
|
42
46
|
|
43
47
|
log.debug 'This message will be stored in new/logs/debug_log.txt'
|
44
48
|
log.error 'This message will be stored in new/logs/error_log.txt'
|
45
|
-
log.puts
|
49
|
+
log.puts 'This message will be stored in new/logs/default_log.txt'
|
50
|
+
|
51
|
+
You can also clear the content of one or all log files:
|
52
|
+
|
53
|
+
log = FLogger::Log.new
|
54
|
+
|
55
|
+
log.debug 'This message will be stored in logs/debug.txt'
|
56
|
+
log.error 'This message will be stored in logs/error.txt'
|
57
|
+
log.puts 'This message will be stored in logs/log.txt'
|
58
|
+
|
59
|
+
log.clear # clears all log files that exist
|
60
|
+
log.clear('debug') # clears the debug log file
|
61
|
+
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'flogger'
|
2
|
+
|
3
|
+
class MessageOutput
|
4
|
+
def initialize
|
5
|
+
@log = FLogger::Log.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def log_message(msg, type = nil)
|
9
|
+
case type
|
10
|
+
when 'debug'
|
11
|
+
@log.debug msg
|
12
|
+
when 'error'
|
13
|
+
@log.error msg
|
14
|
+
else
|
15
|
+
@log.puts msg
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def run_example
|
20
|
+
50.times do |count|
|
21
|
+
log_message "This is logged as a debug message ##{count}", 'debug'
|
22
|
+
log_message "This is logged as an error message ##{count}", 'error'
|
23
|
+
log_message "This is logged as a generic message ##{count}"
|
24
|
+
end
|
25
|
+
|
26
|
+
# clear debug log
|
27
|
+
clear_logs 'debug'
|
28
|
+
|
29
|
+
# clear all logs
|
30
|
+
clear_logs
|
31
|
+
end
|
32
|
+
|
33
|
+
def clear_logs(log_file = nil)
|
34
|
+
@log.clear log_file
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
msg = MessageOutput.new
|
39
|
+
msg.run_example
|
data/examples/custom_output.rb
CHANGED
@@ -8,7 +8,7 @@ class MessageOutput
|
|
8
8
|
@log.configure do |c|
|
9
9
|
c.debug = 'custom/logs/debug_log.txt'
|
10
10
|
c.error = 'custom/logs/error_log.txt'
|
11
|
-
c.default = 'custom/logs/
|
11
|
+
c.default = 'custom/logs/app_log.txt'
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
@@ -25,9 +25,9 @@ class MessageOutput
|
|
25
25
|
|
26
26
|
def run_example
|
27
27
|
50.times do |count|
|
28
|
-
log_message "
|
29
|
-
log_message "
|
30
|
-
log_message "
|
28
|
+
log_message "This is logged as a debug message ##{count}", 'debug'
|
29
|
+
log_message "This is logged as an error message ##{count}", 'error'
|
30
|
+
log_message "This is logged as a generic message ##{count}"
|
31
31
|
end
|
32
32
|
end
|
33
33
|
end
|
data/examples/default_output.rb
CHANGED
@@ -18,9 +18,9 @@ class MessageOutput
|
|
18
18
|
|
19
19
|
def run_example
|
20
20
|
50.times do |count|
|
21
|
-
log_message "
|
22
|
-
log_message "
|
23
|
-
log_message "
|
21
|
+
log_message "This is logged as a debug message ##{count}", 'debug'
|
22
|
+
log_message "This is logged as an error message ##{count}", 'error'
|
23
|
+
log_message "This is logged as a generic message ##{count}"
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
data/flogger.gemspec
CHANGED
@@ -1,24 +1,21 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
1
|
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
|
3
3
|
require "flogger/version"
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
6
6
|
s.name = "flogger"
|
7
7
|
s.version = FLogger::VERSION
|
8
|
+
s.summary = %q{A simple Ruby-based message logger.}
|
9
|
+
s.description = %q{A simple Ruby-based message logger that can output to one or many log files.}
|
10
|
+
|
8
11
|
s.authors = ["Andre Burdette"]
|
9
12
|
s.email = ["andre@nerdstack.com"]
|
10
13
|
s.homepage = "https://github.com/aburdette/flogger"
|
11
|
-
|
12
|
-
s.description = %q{A simple Ruby-based message logging system that outputs to one or many log files.}
|
13
|
-
|
14
|
+
|
14
15
|
s.rubyforge_project = "flogger"
|
15
16
|
|
16
17
|
s.files = `git ls-files`.split("\n")
|
17
18
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
19
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
20
|
s.require_paths = ["lib"]
|
20
|
-
|
21
|
-
# specify any dependencies here; for example:
|
22
|
-
# s.add_development_dependency "rspec"
|
23
|
-
# s.add_runtime_dependency "rest-client"
|
24
21
|
end
|
data/lib/flogger.rb
CHANGED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
class File
|
4
|
+
def self.open_or_create(file, access)
|
5
|
+
begin
|
6
|
+
path = self.dirname(file)
|
7
|
+
FileUtils.mkdir_p(path) unless self.exists?(file)
|
8
|
+
self.open(file, access)
|
9
|
+
rescue Exception => e
|
10
|
+
puts e.message
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.erase_contents(file)
|
15
|
+
if self.exists?(file)
|
16
|
+
f = File.open(file, 'w')
|
17
|
+
f.truncate(0)
|
18
|
+
else
|
19
|
+
puts "File does not exist: #{file}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/flogger/log.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'ostruct'
|
2
|
-
require 'fileutils'
|
3
2
|
|
4
3
|
module FLogger
|
5
4
|
class Log
|
@@ -9,9 +8,9 @@ module FLogger
|
|
9
8
|
@logs = OpenStruct.new(
|
10
9
|
:debug => 'logs/debug.txt',
|
11
10
|
:error => 'logs/error.txt',
|
12
|
-
:warning => 'logs/warning.txt',
|
13
11
|
:notice => 'logs/notice.txt',
|
14
|
-
:
|
12
|
+
:warning => 'logs/warning.txt',
|
13
|
+
:default => 'logs/application.txt'
|
15
14
|
)
|
16
15
|
end
|
17
16
|
|
@@ -21,27 +20,55 @@ module FLogger
|
|
21
20
|
|
22
21
|
# output to debug log, default: logs/debug.txt
|
23
22
|
def debug(msg)
|
24
|
-
output
|
23
|
+
output(open(@logs.debug), msg)
|
25
24
|
end
|
26
25
|
|
27
26
|
# output to error log, default: logs/error.txt
|
28
27
|
def error(msg)
|
29
|
-
output
|
28
|
+
output(open(@logs.error), msg)
|
30
29
|
end
|
31
30
|
|
32
31
|
# output to warning log, default: logs/warning.txt
|
33
32
|
def warning(msg)
|
34
|
-
output
|
33
|
+
output(open(@logs.warning), msg)
|
35
34
|
end
|
36
35
|
|
37
36
|
# output to notice log, default: logs/notice.txt
|
38
37
|
def notice(msg)
|
39
|
-
output
|
38
|
+
output(open(@logs.notice), msg)
|
40
39
|
end
|
41
40
|
|
42
|
-
# output to log, default: logs/
|
41
|
+
# output to log, default: logs/application.txt
|
43
42
|
def puts(msg)
|
44
|
-
output
|
43
|
+
output(open(@logs.default), msg)
|
44
|
+
end
|
45
|
+
|
46
|
+
# clears the contents of log files
|
47
|
+
def clear(log_type = nil)
|
48
|
+
if log_type
|
49
|
+
# clear a single log
|
50
|
+
case log_type
|
51
|
+
when 'debug'
|
52
|
+
clear_log(@logs.debug)
|
53
|
+
when 'error'
|
54
|
+
clear_log(@logs.error)
|
55
|
+
when 'notice'
|
56
|
+
clear_log(@logs.notice)
|
57
|
+
when 'warning'
|
58
|
+
clear_log(@logs.warning)
|
59
|
+
when 'default'
|
60
|
+
clear_log(@logs.default)
|
61
|
+
else
|
62
|
+
$stdout.puts "ERROR: '#{log_type}' is not a valid log type."
|
63
|
+
end
|
64
|
+
else
|
65
|
+
# clear all logs
|
66
|
+
clear_log(@logs.debug)
|
67
|
+
clear_log(@logs.error)
|
68
|
+
clear_log(@logs.notice)
|
69
|
+
clear_log(@logs.warning)
|
70
|
+
clear_log(@logs.default)
|
71
|
+
end
|
45
72
|
end
|
46
73
|
|
47
74
|
private
|
@@ -49,11 +76,9 @@ module FLogger
|
|
49
76
|
# access the needed log file
|
50
77
|
def open(log_file)
|
51
78
|
begin
|
52
|
-
|
53
|
-
FileUtils.mkdir_p(log_path) unless File.exists? log_file
|
54
|
-
File.open(log_file, 'a')
|
79
|
+
File.open_or_create(log_file, 'a')
|
55
80
|
rescue Exception => e
|
56
|
-
|
81
|
+
$stdout.puts "ERROR: #{e.message}"
|
57
82
|
end
|
58
83
|
end
|
59
84
|
|
@@ -64,7 +89,18 @@ module FLogger
|
|
64
89
|
log.puts "[#{Time.now.asctime}] #{msg}"
|
65
90
|
log.close
|
66
91
|
rescue Exception => e
|
67
|
-
|
92
|
+
$stdout.puts "ERROR: #{e.message}"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
# clears log file contents
|
98
|
+
def clear_log(log_file)
|
99
|
+
if File.exists?(log_file)
|
100
|
+
begin
|
101
|
+
File.erase_contents(log_file)
|
102
|
+
rescue Exception => e
|
103
|
+
$stdout.puts "ERROR: #{e.message}"
|
68
104
|
end
|
69
105
|
end
|
70
106
|
end
|
data/lib/flogger/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: flogger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,10 +9,10 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-08-
|
12
|
+
date: 2011-08-21 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
|
-
description: A simple Ruby-based message
|
15
|
-
|
14
|
+
description: A simple Ruby-based message logger that can output to one or many log
|
15
|
+
files.
|
16
16
|
email:
|
17
17
|
- andre@nerdstack.com
|
18
18
|
executables: []
|
@@ -24,10 +24,12 @@ files:
|
|
24
24
|
- Gemfile
|
25
25
|
- README.rdoc
|
26
26
|
- Rakefile
|
27
|
+
- examples/clear_logs.rb
|
27
28
|
- examples/custom_output.rb
|
28
29
|
- examples/default_output.rb
|
29
30
|
- flogger.gemspec
|
30
31
|
- lib/flogger.rb
|
32
|
+
- lib/flogger/ext/file.rb
|
31
33
|
- lib/flogger/log.rb
|
32
34
|
- lib/flogger/version.rb
|
33
35
|
homepage: https://github.com/aburdette/flogger
|
@@ -50,8 +52,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
50
52
|
version: '0'
|
51
53
|
requirements: []
|
52
54
|
rubyforge_project: flogger
|
53
|
-
rubygems_version: 1.8.
|
55
|
+
rubygems_version: 1.8.6
|
54
56
|
signing_key:
|
55
57
|
specification_version: 3
|
56
|
-
summary: A simple Ruby-based message
|
58
|
+
summary: A simple Ruby-based message logger.
|
57
59
|
test_files: []
|