flogger 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,9 +1,11 @@
1
1
  *~
2
2
  .DS_Store
3
+ Thumbs.db
3
4
 
4
5
  *.gem
5
6
  .bundle
6
7
  Gemfile.lock
7
8
 
9
+ db/*
8
10
  pkg/*
9
11
  logs/*
@@ -1,3 +1,8 @@
1
+ *0.2.0* September 3, 2011
2
+ * all log output is now done though the Log#puts method
3
+ * custom log paths are now applied during Log#new initialization
4
+ * most methods have been deprecated, please check the documentation[https://rubygems.org/gems/flogger]
5
+
1
6
  *0.1.2* August 21, 2011
2
7
  * added ability to clear contents of existing log files
3
8
  * changed default log file to 'logs/application.txt'
@@ -1,61 +1,34 @@
1
1
  = FLogger
2
2
 
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.
3
+ FLogger (or File Logger (or FLogger)) is an extremely simple and easy-to-use logger that outputs to one or many files. It was created, originally, as a learning project for myself, but has also become a tool that I continue to develop and use.
4
4
 
5
+ == Installation
5
6
 
6
- == Installing
7
-
8
- FLogger can be downloaded from source or installed as a gem with the command:
7
+ The latest stable release of FLogger can be installed via RubyGems:
9
8
 
10
9
  gem install flogger
11
10
 
11
+ Or you can check out the latest source code via Github:
12
12
 
13
- == Usage and Default
13
+ git clone https://github.com/aburdette/flogger.git
14
14
 
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:
15
+ == Usage and Defaults
16
16
 
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
17
+ 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. To change the default path, specify a different one during initialization:
22
18
 
19
+ @log = FLogger::Log.new do |paths|
20
+ paths.debug = 'new/path/to/debug.log'
21
+ paths.default = 'new/path/to/default.log'
22
+ end
23
23
 
24
24
  == Examples
25
25
 
26
- More detailed examples of how to use FLogger can be found in the examples directory. Still, here is a simple example of how to use FLogger to output messages to default log files:
26
+ There are a few more detailed examples in the examples directory. However, below is a simple example of using FLogger to output log messages:
27
27
 
28
28
  require 'flogger'
29
29
 
30
- log = FLogger::Log.new
31
-
32
- log.debug 'This message will be stored in logs/debug.txt'
33
- log.error 'This message will be stored in logs/error.txt'
34
- log.puts 'This message will be stored in logs/application.txt'
35
-
36
- This example shows how easy it is to change the location FLogger outputs log files:
37
-
38
- log = FLogger::Log.new
39
-
40
- # change a few log locations
41
- log.configure do |c|
42
- c.debug = 'new/logs/debug_log.txt'
43
- c.error = 'new/logs/error_log.txt'
44
- c.default = 'new/logs/default_log.txt'
45
- end
46
-
47
- log.debug 'This message will be stored in new/logs/debug_log.txt'
48
- log.error 'This message will be stored in new/logs/error_log.txt'
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
30
+ @log = FLogger::Log.new
61
31
 
32
+ log.puts :debug, 'This message will output to logs/debug.txt by default.'
33
+ log.puts :error, 'This message will output to logs/error.txt by default.'
34
+ log.puts 'This message will output to logs/application.txt by default.'
@@ -1,39 +1,30 @@
1
1
  require 'flogger'
2
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}"
3
+ class LogFiller
4
+ def initialize
5
+ # initialize with default paths
6
+ @log = FLogger::Log.new
24
7
  end
25
8
 
26
- # clear debug log
27
- clear_logs 'debug'
9
+ def fill
10
+ 100.times do |count|
11
+ @log.puts :debug, "#{count} - This is logged as a debug message."
12
+ @log.puts :error, "#{count} - This is logged as an error message."
13
+ @log.puts "#{count} - This is logged as an application message."
14
+ end
15
+ end
28
16
 
29
- # clear all logs
30
- clear_logs
31
- end
32
-
33
- def clear_logs(log_file = nil)
34
- @log.clear log_file
35
- end
17
+ def clear(type = :all)
18
+ @log.clear type
19
+ end
36
20
  end
37
21
 
38
- msg = MessageOutput.new
39
- msg.run_example
22
+ # create and fill logs
23
+ log_filler = LogFiller.new
24
+ log_filler.fill
25
+
26
+ # clear just the debug log
27
+ log_filler.clear :debug
28
+
29
+ # clear all logs
30
+ log_filler.clear
@@ -1,36 +1,24 @@
1
1
  require 'flogger'
2
2
 
3
- class MessageOutput
4
- def initialize
5
- @log = FLogger::Log.new
6
-
7
- # configure custom log paths
8
- @log.configure do |c|
9
- c.debug = 'custom/logs/debug_log.txt'
10
- c.error = 'custom/logs/error_log.txt'
11
- c.default = 'custom/logs/app_log.txt'
12
- end
13
- end
14
-
15
- def log_message(msg, type = nil)
16
- case type
17
- when 'debug'
18
- @log.debug msg
19
- when 'error'
20
- @log.error msg
21
- else
22
- @log.puts msg
3
+ class LogFiller
4
+ def initialize
5
+ # initialize with custom paths
6
+ @log = FLogger::Log.new do |paths|
7
+ paths.debug = 'custom/log/path/debug.log'
8
+ paths.error = 'custom/log/path/error.log'
9
+ paths.default = 'custom/log/path/application.log'
10
+ end
23
11
  end
24
- end
25
-
26
- def run_example
27
- 50.times do |count|
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}"
12
+
13
+ def fill
14
+ 100.times do |count|
15
+ @log.puts :debug, "#{count} - This is logged as a debug message."
16
+ @log.puts :error, "#{count} - This is logged as an error message."
17
+ @log.puts "#{count} - This is logged as an application message."
18
+ end
31
19
  end
32
- end
33
20
  end
34
21
 
35
- msg = MessageOutput.new
36
- msg.run_example
22
+ # create and fill logs
23
+ log_filler = LogFiller.new
24
+ log_filler.fill
@@ -1,29 +1,20 @@
1
1
  require 'flogger'
2
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
3
+ class LogFiller
4
+ def initialize
5
+ # initialize with default paths
6
+ @log = FLogger::Log.new
16
7
  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}"
8
+
9
+ def fill
10
+ 100.times do |count|
11
+ @log.puts :debug, "#{count} - This is logged as a debug message."
12
+ @log.puts :error, "#{count} - This is logged as an error message."
13
+ @log.puts "#{count} - This is logged as an application message."
14
+ end
24
15
  end
25
- end
26
16
  end
27
17
 
28
- msg = MessageOutput.new
29
- msg.run_example
18
+ # create and fill logs
19
+ log_filler = LogFiller.new
20
+ log_filler.fill
@@ -1,21 +1,23 @@
1
- $:.push File.expand_path("../lib", __FILE__)
2
-
3
- require "flogger/version"
1
+ $:.push File.expand_path('../lib', __FILE__)
2
+ require 'flogger/version'
4
3
 
5
4
  Gem::Specification.new do |s|
6
- s.name = "flogger"
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
-
11
- s.authors = ["Andre Burdette"]
12
- s.email = ["andre@nerdstack.com"]
13
- s.homepage = "https://github.com/aburdette/flogger"
14
-
15
- s.rubyforge_project = "flogger"
16
-
17
- s.files = `git ls-files`.split("\n")
18
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
- s.require_paths = ["lib"]
5
+ s.name = 'flogger'
6
+ s.version = FLogger::VERSION
7
+ s.summary = %q{Simple Ruby-based logger with file output.}
8
+ s.description = %q{Simple Ruby-based logger that can output to one or several log files.}
9
+
10
+ s.authors = [ 'Andre Burdette' ]
11
+ s.email = [ 'andre@nerdstack.com' ]
12
+ s.homepage = 'https://github.com/aburdette/flogger'
13
+
14
+ s.rubyforge_project = 'flogger'
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = [ 'lib' ]
20
+
21
+ s.required_ruby_version = '~> 1.9.2'
22
+ s.add_development_dependency 'rspec'
21
23
  end
@@ -1,6 +1,11 @@
1
1
  require 'flogger/log'
2
2
 
3
3
  module FLogger
4
- autoload :VERSION, 'flogger/version'
5
- autoload :File, 'flogger/ext/file'
4
+ autoload :VERSION, 'flogger/version'
5
+ autoload :File, 'flogger/ext/file'
6
+
7
+ # output a deprecation error message
8
+ def self.output_deprecation(version, method)
9
+ $stderr.puts "Deprecation: #{method} was marked as deprecated as of version #{version}."
10
+ end
6
11
  end
@@ -1,22 +1,14 @@
1
1
  require 'fileutils'
2
2
 
3
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
4
+ # create a file if it doesn't already exist
5
+ def self.open_or_create(file, access)
6
+ begin
7
+ path = File.dirname(file)
8
+ FileUtils.mkdir_p(path) unless File.exists?(file)
9
+ File.open(file, access)
10
+ rescue Exception => e
11
+ $stderr.puts e.message
12
+ end
11
13
  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
14
  end
@@ -1,108 +1,130 @@
1
1
  require 'ostruct'
2
2
 
3
3
  module FLogger
4
- class Log
5
- attr_reader :logs
6
-
7
- def initialize
8
- @logs = OpenStruct.new(
9
- :debug => 'logs/debug.txt',
10
- :error => 'logs/error.txt',
11
- :notice => 'logs/notice.txt',
12
- :warning => 'logs/warning.txt',
13
- :default => 'logs/application.txt'
14
- )
15
- end
16
-
17
- def configure(&block)
18
- block.call(@logs)
19
- end
20
-
21
- # output to debug log, default: logs/debug.txt
22
- def debug(msg)
23
- output(open(@logs.debug), msg)
24
- end
25
-
26
- # output to error log, default: logs/error.txt
27
- def error(msg)
28
- output(open(@logs.error), msg)
29
- end
30
-
31
- # output to warning log, default: logs/warning.txt
32
- def warning(msg)
33
- output(open(@logs.warning), msg)
34
- end
35
-
36
- # output to notice log, default: logs/notice.txt
37
- def notice(msg)
38
- output(open(@logs.notice), msg)
39
- end
40
-
41
- # output to log, default: logs/application.txt
42
- def puts(msg)
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."
4
+ class Log
5
+ attr_reader :logpaths
6
+
7
+ def initialize
8
+ # set default log paths
9
+ @logpaths = OpenStruct.new(
10
+ :debug => 'logs/debug.txt',
11
+ :error => 'logs/error.txt',
12
+ :notice => 'logs/notice.txt',
13
+ :warning => 'logs/warning.txt',
14
+ :default => 'logs/application.txt'
15
+ )
16
+
17
+ # yield to custom log paths
18
+ yield @logpaths if block_given?
63
19
  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
72
- end
73
-
74
- private
75
-
76
- # access the needed log file
77
- def open(log_file)
78
- begin
79
- File.open_or_create(log_file, 'a')
80
- rescue Exception => e
81
- $stdout.puts "ERROR: #{e.message}"
82
- end
83
- end
84
-
85
- # output message to log file
86
- def output(log, msg)
87
- if log
88
- begin
89
- log.puts "[#{Time.now.asctime}] #{msg}"
90
- log.close
91
- rescue Exception => e
92
- $stdout.puts "ERROR: #{e.message}"
20
+
21
+ # deprecated: use Log#new
22
+ # configure log file locations
23
+ def configure(&block)
24
+ FLogger.output_deprecation('0.2.0', 'Log#configure')
93
25
  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}"
26
+
27
+ # deprecated: use Log#puts
28
+ # outputs to debug log
29
+ def debug(message)
30
+ FLogger.output_deprecation('0.2.0', 'Log#debug')
31
+ end
32
+
33
+ # deprecated: use Log#puts
34
+ # outputs to error log
35
+ def error(message)
36
+ FLogger.output_deprecation('0.2.0', 'Log#error')
37
+ end
38
+
39
+ # deprecated: use Log#puts
40
+ # outputs to warning log
41
+ def warning(message)
42
+ FLogger.output_deprecation('0.2.0', 'Log#warning')
43
+ end
44
+
45
+ # deprecated: use Log#puts
46
+ # outputs to notice log
47
+ def notice(message)
48
+ FLogger.output_deprecation('0.2.0', 'Log#notice')
49
+ end
50
+
51
+ # outputs to log file
52
+ # default: logs/application.txt
53
+ def puts(type = :default, message)
54
+ return if message.empty?
55
+
56
+ case type
57
+ when :debug
58
+ output_to_log(@logpaths.debug, message)
59
+
60
+ when :error
61
+ output_to_log(@logpaths.error, message)
62
+
63
+ when :notice
64
+ output_to_log(@logpaths.notice, message)
65
+
66
+ when :warning
67
+ output_to_log(@logpaths.warning, message)
68
+
69
+ when :default
70
+ output_to_log(@logpaths.default, message)
71
+
72
+ else
73
+ $stderr.puts "Log type not supported."
74
+ end
75
+ end
76
+
77
+ def clear(type = :default)
78
+ case type
79
+ when :all
80
+ clear_log_contents @logpaths.debug
81
+ clear_log_contents @logpaths.error
82
+ clear_log_contents @logpaths.notice
83
+ clear_log_contents @logpaths.warning
84
+ clear_log_contents @logpaths.default
85
+
86
+ when :debug
87
+ clear_log_contents @logpaths.debug
88
+
89
+ when :error
90
+ clear_log_contents @logpaths.error
91
+
92
+ when :notice
93
+ clear_log_contents @logpaths.notice
94
+
95
+ when :warning
96
+ clear_log_contents @logpaths.warning
97
+
98
+ when :default
99
+ clear_log_contents @logpaths.default
100
+
101
+ else
102
+ $stderr.puts "Log type not supported."
103
+ end
104
+ end
105
+
106
+ private
107
+
108
+ def output_to_log(file, message)
109
+ return if message.empty?
110
+
111
+ begin
112
+ log_file = File.open_or_create(file, 'a')
113
+ log_file.puts "[#{Time.now.asctime}] #{message}"
114
+ log_file.close
115
+ rescue Exception => e
116
+ $stderr.puts e.message
117
+ end
118
+ end
119
+
120
+ def clear_log_contents(file)
121
+ if File.exists? file
122
+ f = File.open(file, 'w')
123
+ f.truncate(0)
124
+ f.close
125
+ else
126
+ $stderr.puts "Could not clear log, file does not exist: #{file}"
127
+ end
104
128
  end
105
- end
106
129
  end
107
- end
108
130
  end
@@ -1,3 +1,3 @@
1
1
  module FLogger
2
- VERSION = "0.1.2"
2
+ VERSION = '0.2.0'
3
3
  end
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.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,10 +9,20 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-21 00:00:00.000000000Z
13
- dependencies: []
14
- description: A simple Ruby-based message logger that can output to one or many log
15
- files.
12
+ date: 2011-09-03 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &84575300 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *84575300
25
+ description: Simple Ruby-based logger that can output to one or several log files.
16
26
  email:
17
27
  - andre@nerdstack.com
18
28
  executables: []
@@ -41,9 +51,9 @@ require_paths:
41
51
  required_ruby_version: !ruby/object:Gem::Requirement
42
52
  none: false
43
53
  requirements:
44
- - - ! '>='
54
+ - - ~>
45
55
  - !ruby/object:Gem::Version
46
- version: '0'
56
+ version: 1.9.2
47
57
  required_rubygems_version: !ruby/object:Gem::Requirement
48
58
  none: false
49
59
  requirements:
@@ -52,8 +62,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
62
  version: '0'
53
63
  requirements: []
54
64
  rubyforge_project: flogger
55
- rubygems_version: 1.8.6
65
+ rubygems_version: 1.8.10
56
66
  signing_key:
57
67
  specification_version: 3
58
- summary: A simple Ruby-based message logger.
68
+ summary: Simple Ruby-based logger with file output.
59
69
  test_files: []