daily_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.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.rspec +2 -0
- data/.travis.yml +8 -0
- data/Gemfile +14 -0
- data/LICENSE.txt +22 -0
- data/README.md +38 -0
- data/Rakefile +12 -0
- data/daily_logger.gemspec +19 -0
- data/example/lotate_process_safe_check.rb +33 -0
- data/example/lotate_thread_safe_check.rb +33 -0
- data/example/thread_safe_check.rb +14 -0
- data/lib/daily_logger/adapter/file.rb +82 -0
- data/lib/daily_logger/adapter.rb +46 -0
- data/lib/daily_logger/formatter.rb +58 -0
- data/lib/daily_logger.rb +134 -0
- data/spec/daily_logger_spec.rb +136 -0
- data/spec/spec_helper.rb +11 -0
- metadata +62 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1ae66ffd5b9886b69f59226a9adea89dd3f6b7b9
|
4
|
+
data.tar.gz: 69aa1d917eee5b8d8ab5d66ab741f71eedf548ed
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fe9e39ee297d205696f91ca6be3c50b9047883743c15270c0c644381bc1b718d2e87a984220cc1c8b8985aa991d9e67ac31b92110cd4b7805f0993375b1d605a
|
7
|
+
data.tar.gz: 6b3980e648aa0873335633002d9b92eed6fbc7ac4aaef5ad6b2f6c5e7ba495ed1fb7dc1aa4f83bd5e8ee2e89fd9ba1b1748bd052872253e49741f615f5d82d02
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in file_alternative_logger.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
group :test do
|
7
|
+
gem 'bundler'
|
8
|
+
gem 'rake'
|
9
|
+
gem 'coveralls', require: false
|
10
|
+
gem 'rspec'
|
11
|
+
gem 'timecop'
|
12
|
+
gem 'parallel'
|
13
|
+
end
|
14
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 SpringMT
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# DailyLogger
|
2
|
+
[](https://travis-ci.org/SpringMT/daily_logger)
|
3
|
+
[](https://coveralls.io/r/SpringMT/daily_logger)
|
4
|
+
|
5
|
+
* Yet Another Logger for ruby
|
6
|
+
* daily lotate logger
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'daily_logger'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install daily_logger
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
```
|
25
|
+
require 'daily_logger'
|
26
|
+
logger = DailyLogger.new('/your/directory/filename')
|
27
|
+
logger.info('info------')
|
28
|
+
# output /your/directory/filename.info.log.YYYYMMDD
|
29
|
+
```
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
1. Fork it
|
34
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
35
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
36
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
37
|
+
5. Create new Pull Request
|
38
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.name = "daily_logger"
|
7
|
+
gem.version = "0.0.1"
|
8
|
+
gem.authors = ["Spring_MT"]
|
9
|
+
gem.email = ["today.is.sky.blue.sky@gmail.com"]
|
10
|
+
gem.description = %q{Write a gem description}
|
11
|
+
gem.summary = %q{daily lotate logger}
|
12
|
+
gem.homepage = "https://github.com/SpringMT/daily_logger"
|
13
|
+
|
14
|
+
gem.files = `git ls-files`.split($/)
|
15
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
16
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
17
|
+
gem.require_paths = ["lib"]
|
18
|
+
end
|
19
|
+
|
@@ -0,0 +1,33 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
2
|
+
|
3
|
+
require 'daily_logger'
|
4
|
+
require 'parallel'
|
5
|
+
require 'timecop'
|
6
|
+
require 'test/unit'
|
7
|
+
|
8
|
+
Timecop.scale(24 * 60 * 60)
|
9
|
+
|
10
|
+
$proc_num = 2
|
11
|
+
$execute_num = 10000
|
12
|
+
|
13
|
+
logger = DailyLogger.new("example/log/lotate_test_process")
|
14
|
+
Parallel.map(['a', 'b'], :in_processes => $proc_num) do |letter|
|
15
|
+
$execute_num.times do
|
16
|
+
logger.info letter * 5000
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
$total_num = `wc -l example/log/lotate_test_process*`.split("\n").map(&:strip).grep(/\stotal\z/).first.split(' ').first.to_i
|
21
|
+
p "Expected total line num #{$execute_num * $proc_num}"
|
22
|
+
p "Actually total line num #{$total_num}"
|
23
|
+
|
24
|
+
|
25
|
+
class DailyLoggerTC < Test::Unit::TestCase
|
26
|
+
def test_logger
|
27
|
+
assert_equal($execute_num * $proc_num, $total_num)
|
28
|
+
end
|
29
|
+
def teardown
|
30
|
+
p 'rm -rf example/log/*'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
@@ -0,0 +1,33 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
2
|
+
|
3
|
+
require 'daily_logger'
|
4
|
+
require 'parallel'
|
5
|
+
require 'timecop'
|
6
|
+
require 'test/unit'
|
7
|
+
|
8
|
+
Timecop.scale(24 * 60 * 60)
|
9
|
+
|
10
|
+
$proc_num = 2
|
11
|
+
$execute_num = 10000
|
12
|
+
|
13
|
+
logger = DailyLogger.new("example/log/lotate_test_thread")
|
14
|
+
Parallel.map(['a', 'b'], :in_threads => $proc_num) do |letter|
|
15
|
+
$execute_num.times do
|
16
|
+
logger.info letter * 5000
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
$total_num = `wc -l example/log/lotate_test_thread*`.split("\n").map(&:strip).grep(/\stotal\z/).first.split(' ').first.to_i
|
21
|
+
p "Expected total line num #{$execute_num * $proc_num}"
|
22
|
+
p "Actually total line num #{$total_num}"
|
23
|
+
|
24
|
+
class DailyLoggerTC < Test::Unit::TestCase
|
25
|
+
def test_logger
|
26
|
+
assert_equal($execute_num * $proc_num, $total_num)
|
27
|
+
end
|
28
|
+
def teardown
|
29
|
+
p 'rm -rf example/log/*'
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
2
|
+
|
3
|
+
require 'daily_logger'
|
4
|
+
require 'parallel'
|
5
|
+
|
6
|
+
logger = DailyLogger.new("example/log/test")
|
7
|
+
Parallel.map(['a', 'b'], :in_threads => 2) do |letter|
|
8
|
+
3000.times do
|
9
|
+
logger.info letter * 5000
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# egrep -e 'ab' -e 'ba' example/log/test.log
|
14
|
+
# これはまざらない
|
@@ -0,0 +1,82 @@
|
|
1
|
+
# writeするときに逐次open closeする
|
2
|
+
require 'monitor'
|
3
|
+
|
4
|
+
class DailyLogger
|
5
|
+
class Adapter
|
6
|
+
class File
|
7
|
+
|
8
|
+
class LogFileMutex
|
9
|
+
include MonitorMixin
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(level, path)
|
13
|
+
@path = path
|
14
|
+
@today = Time.now.strftime "%Y%m%d"
|
15
|
+
@timestamp_path = "#{@path}.#{level}.log.#{@today}"
|
16
|
+
# DIRの判定はここでするか?
|
17
|
+
@mutex = LogFileMutex.new
|
18
|
+
@log = open_logfile(@timestamp_path)
|
19
|
+
end
|
20
|
+
|
21
|
+
def write(level, msg)
|
22
|
+
begin
|
23
|
+
@mutex.synchronize do
|
24
|
+
if @log.nil? || !same_date?
|
25
|
+
begin
|
26
|
+
@today = Time.now.strftime "%Y%m%d"
|
27
|
+
@timestamp_path = "#{@path}.#{level}.log.#{@today}"
|
28
|
+
@log.close rescue nil
|
29
|
+
@log = create_logfile(@timestamp_path)
|
30
|
+
rescue
|
31
|
+
warn("log shifting failed. #{$!}")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
begin
|
36
|
+
@log.write msg
|
37
|
+
rescue
|
38
|
+
warn("log writing failed. #{$!}")
|
39
|
+
end
|
40
|
+
end
|
41
|
+
rescue Exception => ignored
|
42
|
+
warn("log writing failed. #{ignored}")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def close
|
47
|
+
if !@log.nil? && !@log.closed?
|
48
|
+
@log.close
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
# ファイルがない場合はnilを返す
|
54
|
+
def open_logfile(filename)
|
55
|
+
return nil unless ::FileTest.exist? filename
|
56
|
+
# ファイルは作らない
|
57
|
+
f = ::File.open filename, (::File::WRONLY | ::File::APPEND)
|
58
|
+
#f.binmode
|
59
|
+
f.sync = true
|
60
|
+
f
|
61
|
+
end
|
62
|
+
|
63
|
+
def create_logfile(filename)
|
64
|
+
begin
|
65
|
+
f = ::File.open filename, (::File::WRONLY | ::File::APPEND | ::File::CREAT | ::File::EXCL)
|
66
|
+
#f.binmode
|
67
|
+
f.sync = true
|
68
|
+
rescue Errno::EEXIST
|
69
|
+
f = open_logfile(filename)
|
70
|
+
end
|
71
|
+
f
|
72
|
+
end
|
73
|
+
|
74
|
+
def same_date?
|
75
|
+
@today == Time.now.strftime("%Y%m%d")
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require File.expand_path('../adapter/file', __FILE__)
|
2
|
+
|
3
|
+
class DailyLogger
|
4
|
+
class Adapter
|
5
|
+
|
6
|
+
def initialize(level, name)
|
7
|
+
@log_adapters = set_adaptor(level, name)
|
8
|
+
end
|
9
|
+
|
10
|
+
def write(level, msg)
|
11
|
+
@log_adapters.each do |adapter|
|
12
|
+
adapter.write(level, msg)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def close
|
17
|
+
@log_adapters.each do |adapter|
|
18
|
+
adapter.close
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
def config
|
24
|
+
config = {
|
25
|
+
debug: [DailyLogger::Adapter::File],
|
26
|
+
info: [DailyLogger::Adapter::File],
|
27
|
+
warn: [DailyLogger::Adapter::File],
|
28
|
+
error: [DailyLogger::Adapter::File],
|
29
|
+
fatal: [DailyLogger::Adapter::File],
|
30
|
+
unknown: [DailyLogger::Adapter::File],
|
31
|
+
}
|
32
|
+
config
|
33
|
+
end
|
34
|
+
|
35
|
+
def set_adaptor(level, name)
|
36
|
+
adapters = Array.new
|
37
|
+
config[level].each do |adapter|
|
38
|
+
adapters.push adapter.new(level, name)
|
39
|
+
end
|
40
|
+
adapters
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
|
@@ -0,0 +1,58 @@
|
|
1
|
+
class DailyLogger
|
2
|
+
# Default formatter for log messages
|
3
|
+
class Formatter
|
4
|
+
Format = "[%s]\t%s\n"
|
5
|
+
|
6
|
+
attr_accessor :datetime_format
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@datetime_format = nil
|
10
|
+
end
|
11
|
+
|
12
|
+
def call(time, msg, block_msg)
|
13
|
+
Format % [format_datetime(time), make_msg(msg, block_msg)]
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
def format_datetime(time)
|
18
|
+
if @datetime_format.nil?
|
19
|
+
time.strftime "%Y/%m/%d %H:%M:%S"
|
20
|
+
else
|
21
|
+
time.strftime @datetime_format
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def make_msg(*args)
|
26
|
+
msgs = Array.new
|
27
|
+
args.each do |msg|
|
28
|
+
str = msg2str msg
|
29
|
+
next if str.nil?
|
30
|
+
msgs.push str
|
31
|
+
end
|
32
|
+
msgs.join "\t"
|
33
|
+
end
|
34
|
+
|
35
|
+
def msg2str(msg)
|
36
|
+
return nil if msg.nil?
|
37
|
+
return msg if msg.kind_of? ::String
|
38
|
+
all_msg = []
|
39
|
+
msg.each do |m|
|
40
|
+
case m
|
41
|
+
when ::String
|
42
|
+
all_msg << m
|
43
|
+
when ::Array
|
44
|
+
all_msg << m
|
45
|
+
when ::Exception
|
46
|
+
short_backtrace = m.backtrace[0,4]
|
47
|
+
all_msg << [m.message, short_backtrace]
|
48
|
+
else
|
49
|
+
all_msg << m.inspect
|
50
|
+
end
|
51
|
+
end
|
52
|
+
all_msg.flatten
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
|
data/lib/daily_logger.rb
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
require File.expand_path('daily_logger/formatter', __dir__)
|
2
|
+
require File.expand_path('daily_logger/adapter', __dir__)
|
3
|
+
require File.expand_path('daily_logger/adapter/file', __dir__)
|
4
|
+
|
5
|
+
class DailyLogger
|
6
|
+
|
7
|
+
# Logging severity.
|
8
|
+
module Severity
|
9
|
+
# Low-level information, mostly for developers
|
10
|
+
DEBUG = 0
|
11
|
+
# generic, useful information about system operation
|
12
|
+
INFO = 1
|
13
|
+
# a warning
|
14
|
+
WARN = 2
|
15
|
+
# a handleable error condition
|
16
|
+
ERROR = 3
|
17
|
+
# an unhandleable error that results in a program crash
|
18
|
+
FATAL = 4
|
19
|
+
# an unknown message that should always be logged
|
20
|
+
UNKNOWN = 5
|
21
|
+
end
|
22
|
+
include Severity
|
23
|
+
|
24
|
+
# Logging severity threshold (e.g. <tt>Logger::INFO</tt>).
|
25
|
+
attr_accessor :level
|
26
|
+
alias sev_threshold level
|
27
|
+
alias sev_threshold= level=
|
28
|
+
|
29
|
+
attr_accessor :formatter
|
30
|
+
attr_accessor :device
|
31
|
+
|
32
|
+
def initialize(name = nil)
|
33
|
+
@name = name || 'all'
|
34
|
+
@level = DEBUG
|
35
|
+
@default_formatter = DailyLogger::Formatter.new
|
36
|
+
@formatter = nil
|
37
|
+
|
38
|
+
@log_adaptor = {}
|
39
|
+
sev_label.each do |level|
|
40
|
+
@log_adaptor[level] = DailyLogger::Adapter.new(level, @name)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def append(severity, msg = nil, &block)
|
45
|
+
severity ||= UNKNOWN
|
46
|
+
|
47
|
+
if @log_adaptor.nil? or severity < @level
|
48
|
+
return true
|
49
|
+
end
|
50
|
+
|
51
|
+
block_msg = nil
|
52
|
+
if block_given?
|
53
|
+
block_msg = yield
|
54
|
+
end
|
55
|
+
|
56
|
+
log_level = sev_label[severity]
|
57
|
+
if @log_adaptor[log_level].nil?
|
58
|
+
@log_adaptor[log_level] = DailyLogger::Adapter.new(log_level, @name)
|
59
|
+
end
|
60
|
+
|
61
|
+
@log_adaptor[log_level].write(log_level, format_message(Time.now, msg, block_msg))
|
62
|
+
true
|
63
|
+
end
|
64
|
+
alias log append
|
65
|
+
|
66
|
+
def debug(*msg, &block)
|
67
|
+
append(DEBUG, msg, &block)
|
68
|
+
end
|
69
|
+
|
70
|
+
def info(*msg, &block)
|
71
|
+
append(INFO, msg, &block)
|
72
|
+
end
|
73
|
+
|
74
|
+
def warn(*msg, &block)
|
75
|
+
#msg = called_from.concat msg
|
76
|
+
append(WARN, msg, &block)
|
77
|
+
end
|
78
|
+
|
79
|
+
def error(*msg, &block)
|
80
|
+
#msg = called_from.concat msg
|
81
|
+
append(ERROR, msg, &block)
|
82
|
+
end
|
83
|
+
|
84
|
+
def fatal(*msg, &block)
|
85
|
+
#msg = called_from.concat msg
|
86
|
+
append(FATAL, msg, &block)
|
87
|
+
end
|
88
|
+
|
89
|
+
def unknown(*msg, &block)
|
90
|
+
append(UNKNOWN, msg, &block)
|
91
|
+
end
|
92
|
+
|
93
|
+
# Returns +true+ iff the current severity level allows for the printing of
|
94
|
+
# +DEBUG+ messages.
|
95
|
+
def debug?; @level <= DEBUG; end
|
96
|
+
|
97
|
+
# Returns +true+ iff the current severity level allows for the printing of
|
98
|
+
# +INFO+ messages.
|
99
|
+
def info?; @level <= INFO; end
|
100
|
+
|
101
|
+
# Returns +true+ iff the current severity level allows for the printing of
|
102
|
+
# +WARN+ messages.
|
103
|
+
def warn?; @level <= WARN; end
|
104
|
+
|
105
|
+
# Returns +true+ iff the current severity level allows for the printing of
|
106
|
+
# +ERROR+ messages.
|
107
|
+
def error?; @level <= ERROR; end
|
108
|
+
|
109
|
+
# Returns +true+ iff the current severity level allows for the printing of
|
110
|
+
# +FATAL+ messages.
|
111
|
+
def fatal?; @level <= FATAL; end
|
112
|
+
|
113
|
+
def close
|
114
|
+
sev_label.each do |level|
|
115
|
+
next if @log_adaptor[level].nil?
|
116
|
+
@log_adaptor[level].close
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
private
|
121
|
+
def sev_label; [:debug, :info, :warn, :error, :fatal, :unknown]; end
|
122
|
+
|
123
|
+
def called_from
|
124
|
+
caller(1)[1] =~ /(.*?):(\d+)(:in `(.*)')?/
|
125
|
+
file_name, line_num, function = $1, $2, $3
|
126
|
+
return [file_name, line_num, function]
|
127
|
+
end
|
128
|
+
|
129
|
+
def format_message(datetime, msg, block_msg)
|
130
|
+
(@formatter || @default_formatter).call(datetime, msg, block_msg)
|
131
|
+
end
|
132
|
+
|
133
|
+
end
|
134
|
+
|
@@ -0,0 +1,136 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
4
|
+
require 'daily_logger'
|
5
|
+
|
6
|
+
describe DailyLogger do
|
7
|
+
let(:default_instance) { described_class.new }
|
8
|
+
let(:log_dir) { "#{File.dirname(__FILE__)}/log" }
|
9
|
+
let(:today) {Time.now.strftime "%Y%m%d"}
|
10
|
+
let(:now) {Time.now.strftime "%Y/%m/%d %H:%M:%S"}
|
11
|
+
|
12
|
+
before do
|
13
|
+
Dir.mkdir(log_dir)
|
14
|
+
Timecop.freeze(Time.now)
|
15
|
+
end
|
16
|
+
|
17
|
+
after do
|
18
|
+
# サブディレクトリを階層が深い順にソートした配列を作成
|
19
|
+
dirlist = Dir::glob(log_dir + "**/").sort do |a,b|
|
20
|
+
b.split('/').size <=> a.split('/').size
|
21
|
+
end
|
22
|
+
|
23
|
+
dirlist.each do |d|
|
24
|
+
Dir::foreach(d) do |f|
|
25
|
+
File::delete(d+f) if ! (/\.+$/ =~ f)
|
26
|
+
end
|
27
|
+
Dir::rmdir(d)
|
28
|
+
end
|
29
|
+
Timecop.return
|
30
|
+
end
|
31
|
+
|
32
|
+
describe 'default' do
|
33
|
+
subject { default_instance }
|
34
|
+
it do
|
35
|
+
expect(default_instance.level).to eq DailyLogger::DEBUG
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe :debug do
|
40
|
+
context 'default' do
|
41
|
+
subject { DailyLogger.new("#{log_dir}/test") }
|
42
|
+
it do
|
43
|
+
subject.debug("test")
|
44
|
+
expect(File.read("#{log_dir}/test.debug.log.#{today}")).to eq "[#{now}]\ttest\n"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
describe :info do
|
51
|
+
context 'default' do
|
52
|
+
subject { DailyLogger.new("#{log_dir}/test") }
|
53
|
+
it do
|
54
|
+
subject.info("test")
|
55
|
+
expect(File.read("#{log_dir}/test.info.log.#{today}")).to eq "[#{now}]\ttest\n"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'Array msg' do
|
60
|
+
subject { DailyLogger.new("#{log_dir}/test") }
|
61
|
+
it do
|
62
|
+
subject.info("foo", "bar", "buzz")
|
63
|
+
expect(File.read("#{log_dir}/test.info.log.#{today}")).to eq "[#{now}]\tfoo\tbar\tbuzz\n"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'Exception msg' do
|
68
|
+
subject { DailyLogger.new("#{log_dir}/test") }
|
69
|
+
it do
|
70
|
+
begin
|
71
|
+
raise "error test"
|
72
|
+
rescue => e
|
73
|
+
subject.info(e)
|
74
|
+
end
|
75
|
+
expect(File.read("#{log_dir}/test.info.log.#{today}")).to match(/\A\[#{now}\]\terror test\t/)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe :warn do
|
81
|
+
context 'default' do
|
82
|
+
subject { DailyLogger.new("#{log_dir}/test") }
|
83
|
+
it do
|
84
|
+
subject.warn("test")
|
85
|
+
expect(File.read("#{log_dir}/test.warn.log.#{today}")).to eq "[#{now}]\ttest\n"
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe :error do
|
91
|
+
context 'default' do
|
92
|
+
subject { DailyLogger.new("#{log_dir}/test") }
|
93
|
+
it do
|
94
|
+
subject.error("test")
|
95
|
+
expect(File.read("#{log_dir}/test.error.log.#{today}")).to eq "[#{now}]\ttest\n"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
describe :fatal do
|
101
|
+
context 'default' do
|
102
|
+
subject { DailyLogger.new("#{log_dir}/test") }
|
103
|
+
it do
|
104
|
+
subject.fatal("test")
|
105
|
+
expect(File.read("#{log_dir}/test.fatal.log.#{today}")).to eq "[#{now}]\ttest\n"
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
describe :unknown do
|
111
|
+
context 'default' do
|
112
|
+
subject { DailyLogger.new("#{log_dir}/test") }
|
113
|
+
it do
|
114
|
+
subject.unknown("test")
|
115
|
+
expect(File.read("#{log_dir}/test.unknown.log.#{today}")).to eq "[#{now}]\ttest\n"
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
describe 'lotate log' do
|
121
|
+
context 'default' do
|
122
|
+
subject { DailyLogger.new("#{log_dir}/test") }
|
123
|
+
it do
|
124
|
+
subject.info("test")
|
125
|
+
Timecop.freeze(Time.now + 24 * 60 * 60)
|
126
|
+
subject.info("test")
|
127
|
+
yesterday = (Time.now - 24 * 60 * 60).strftime "%Y%m%d"
|
128
|
+
one_day_ago = (Time.now - 24 * 60 * 60).strftime "%Y/%m/%d %H:%M:%S"
|
129
|
+
expect(File.read("#{log_dir}/test.info.log.#{yesterday}")).to eq "[#{one_day_ago}]\ttest\n"
|
130
|
+
expect(File.read("#{log_dir}/test.info.log.#{today}")).to eq "[#{now}]\ttest\n"
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
136
|
+
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: daily_logger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Spring_MT
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-11-13 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Write a gem description
|
14
|
+
email:
|
15
|
+
- today.is.sky.blue.sky@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- .gitignore
|
21
|
+
- .rspec
|
22
|
+
- .travis.yml
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- daily_logger.gemspec
|
28
|
+
- example/lotate_process_safe_check.rb
|
29
|
+
- example/lotate_thread_safe_check.rb
|
30
|
+
- example/thread_safe_check.rb
|
31
|
+
- lib/daily_logger.rb
|
32
|
+
- lib/daily_logger/adapter.rb
|
33
|
+
- lib/daily_logger/adapter/file.rb
|
34
|
+
- lib/daily_logger/formatter.rb
|
35
|
+
- spec/daily_logger_spec.rb
|
36
|
+
- spec/spec_helper.rb
|
37
|
+
homepage: https://github.com/SpringMT/daily_logger
|
38
|
+
licenses: []
|
39
|
+
metadata: {}
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 2.0.3
|
57
|
+
signing_key:
|
58
|
+
specification_version: 4
|
59
|
+
summary: daily lotate logger
|
60
|
+
test_files:
|
61
|
+
- spec/daily_logger_spec.rb
|
62
|
+
- spec/spec_helper.rb
|