logspot 0.1.0 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8d192db9fe3ebecb045035e87b065d63d777a306
4
- data.tar.gz: 622799d4fd8b4fc93c7c74a1e4fecf73e61ea427
3
+ metadata.gz: 3ecd1b65ec0d5cb4c8853789e1a82c5eb8c7cf0f
4
+ data.tar.gz: 0a7900dee0319b598727cfbf017cdcb40f15d149
5
5
  SHA512:
6
- metadata.gz: 749bd7ba5481d29989ea90f64365045aefb5919d4d3e63c20d4c16c78153cbf61f5b434f416a5fce1f0b2aa7f506ec03f166d655c46bde99386e91bdfeac80a5
7
- data.tar.gz: 2ccc27932373cb84f072b63872e0baa94dbab149ffc72fbe429b8b4d619934460e9c1a48edcd069cbee72731add3e6f4010cf09193c205f6ca967938e167ce94
6
+ metadata.gz: 8ed97516bd4abdb091a988c0f6a4562b618b012e810f43eeb32527e808c35f2bb99f33f9997ef0742220f2ee4187142d74e6f48f93ec95dccf7ba3efaf5f2828
7
+ data.tar.gz: c437f59555bb6107129f022c57e3ba1004a5722f521a2a155f3e2ff6ee48fe96defc7f55ef5d5c7bde6adcce733eb756c30c22fa189c3a0dd3cc7c803933ada4
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ /*.gem
2
+ /tmp
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.2
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,41 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ logspot (0.2.0)
5
+ activesupport (>= 4)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (4.1.8)
11
+ i18n (~> 0.6, >= 0.6.9)
12
+ json (~> 1.7, >= 1.7.7)
13
+ minitest (~> 5.1)
14
+ thread_safe (~> 0.1)
15
+ tzinfo (~> 1.1)
16
+ diff-lcs (1.2.5)
17
+ i18n (0.6.11)
18
+ json (1.8.1)
19
+ minitest (5.4.3)
20
+ rspec (3.1.0)
21
+ rspec-core (~> 3.1.0)
22
+ rspec-expectations (~> 3.1.0)
23
+ rspec-mocks (~> 3.1.0)
24
+ rspec-core (3.1.7)
25
+ rspec-support (~> 3.1.0)
26
+ rspec-expectations (3.1.2)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.1.0)
29
+ rspec-mocks (3.1.3)
30
+ rspec-support (~> 3.1.0)
31
+ rspec-support (3.1.2)
32
+ thread_safe (0.3.4)
33
+ tzinfo (1.2.2)
34
+ thread_safe (~> 0.1)
35
+
36
+ PLATFORMS
37
+ ruby
38
+
39
+ DEPENDENCIES
40
+ logspot!
41
+ rspec
data/README.md ADDED
File without changes
data/lib/initialize.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'active_support/all'
5
+
6
+ ActiveSupport::Dependencies.autoload_paths << File.expand_path('../../lib', __FILE__)
@@ -0,0 +1,19 @@
1
+ class LoGspot::Output::File
2
+ def initialize(file_name)
3
+ FileUtils.mkdir_p(File.dirname(file_name))
4
+ @file = File.open(file_name, 'a')
5
+ end
6
+
7
+ def puts(data)
8
+ file.puts(data[:message])
9
+ file.flush
10
+ end
11
+
12
+ def finalize
13
+ @file.close
14
+ end
15
+
16
+ private
17
+
18
+ attr_reader :file
19
+ end
@@ -0,0 +1,14 @@
1
+ class LoGspot::Output::Wrap
2
+ def initialize(wrapper, output)
3
+ @wrapper = wrapper
4
+ @output = output
5
+ end
6
+
7
+ def puts(data)
8
+ wrapper.call(output, data)
9
+ end
10
+
11
+ private
12
+
13
+ attr_reader :wrapper, :output
14
+ end
data/lib/logspot.rb ADDED
@@ -0,0 +1,109 @@
1
+ require_relative 'initialize'
2
+
3
+ class LoGspot
4
+ LOG_LEVELS = %w(DEBUG INFO WARN ERROR FATAL)
5
+
6
+ def initialize(file_name, wrapper = nil)
7
+ wrapper = ->(output, data) {
8
+ base = "[#{Time.now.strftime('%Y/%m/%d %H:%M:%S')} #{level[0]}] "
9
+ if data[:space]
10
+ base = ' ' * base.length
11
+ end
12
+ output.puts(message: "#{base}#{data[:message]}")
13
+ }
14
+ @file = Output::File.new(file_name)
15
+ @output = @original_output = Output::Wrap.new(wrapper, @file)
16
+ @level = nil
17
+ end
18
+
19
+ def tagged(tag, &block)
20
+ wrap_output(block) do |output, data|
21
+ base = tag
22
+ if space = !!data[:space]
23
+ base = ' ' * base.length
24
+ end
25
+ output.puts(data.merge(message: "#{base}#{data[:message]}", space: space))
26
+ end
27
+ end
28
+
29
+ def tagged_list(tag, *args, &block)
30
+ first = true
31
+ wrap_output(block, *args) do |output, data|
32
+ message = data[:message]
33
+ base = tag
34
+ if data[:space] || !first
35
+ base = ' ' * base.length
36
+ end
37
+ if first
38
+ output.puts(data.merge(message: "#{base}#{message}", space: false))
39
+ first = false
40
+ else
41
+ output.puts(data.merge(message: "#{base}#{message}", space: true))
42
+ end
43
+ end
44
+ end
45
+
46
+ def hash(h, &block)
47
+ len = (h.keys.map(&:length).max || 0) + 2
48
+ h.each do |key, value|
49
+ tagged_list("#{key}: ".ljust(len), key, value, &block)
50
+ end
51
+ end
52
+
53
+ def value(v, options = {})
54
+ if v.is_a?(Hash)
55
+ hash(v) do |k, v|
56
+ value(v, options)
57
+ end
58
+ elsif v.is_a?(Array)
59
+ h = Hash[v.map.with_index do |v, i|
60
+ [i.to_s, v]
61
+ end]
62
+ value(h, options)
63
+ else
64
+ if p = options[:split_proc]
65
+ p.call(v.to_s).each do |line|
66
+ write(@level, line)
67
+ end
68
+ elsif max = options[:max_columns]
69
+ v.to_s.split('').each_slice(max).map { |x| x.join('') }.each do |line|
70
+ write(@level, line)
71
+ end
72
+ else
73
+ write(@level, v)
74
+ end
75
+ end
76
+ end
77
+
78
+ def untagged(&block)
79
+ previous_output, @output = output, original_output
80
+ block.call
81
+ @output = previous_output
82
+ end
83
+
84
+ LOG_LEVELS.each do |level|
85
+ define_method(level.downcase) do |*args, &block|
86
+ write(level, *args, &block)
87
+ end
88
+ end
89
+
90
+ def write(l, *args, &block)
91
+ @level = l
92
+ output.puts(message: args[0], args: args, arg_block: block)
93
+ end
94
+
95
+ def finalize
96
+ @file.finalize
97
+ end
98
+
99
+ private
100
+
101
+ attr_reader :original_output, :output, :level
102
+
103
+ def wrap_output(block, *args, &wrapper)
104
+ previous_output, @output = output, Output::Wrap.new(wrapper, output)
105
+ res = block.call(*args)
106
+ @output = previous_output
107
+ res
108
+ end
109
+ end
data/logspot.gemspec ADDED
@@ -0,0 +1,13 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logspot'
3
+ s.version = '0.2.0'
4
+ s.authors = ['Tetsuri Moriya']
5
+ s.email = ['tetsuri.moriya@gmail.com']
6
+ s.summary = 'Logger'
7
+ s.description = 'Logger with various output forms'
8
+ s.homepage = 'https://github.com/pandora2000/'
9
+ s.license = 'MIT'
10
+ s.files = `git ls-files`.split("\n")
11
+ s.add_development_dependency 'rspec', '>= 0'
12
+ s.add_runtime_dependency 'activesupport', '>= 4'
13
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe LoGspot do
4
+ let(:filename) { File.expand_path('../../tmp/spec.log', __FILE__) }
5
+ let(:logger) { LoGspot.new(filename) }
6
+ let(:read) { -> { File.read(filename) } }
7
+
8
+ describe '#write' do
9
+ it 'should output' do
10
+ logger.info('test')
11
+ expect(read.call).to include 'test'
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ require 'bundler/setup'
2
+ Bundler.require
3
+
4
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logspot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tetsuri Moriya
@@ -10,6 +10,20 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2014-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
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'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: activesupport
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -30,8 +44,20 @@ email:
30
44
  executables: []
31
45
  extensions: []
32
46
  extra_rdoc_files: []
33
- files: []
34
- homepage: https://github.com/pandora2000/logspot
47
+ files:
48
+ - ".gitignore"
49
+ - ".ruby-version"
50
+ - Gemfile
51
+ - Gemfile.lock
52
+ - README.md
53
+ - lib/initialize.rb
54
+ - lib/lo_gspot/output/file.rb
55
+ - lib/lo_gspot/output/wrap.rb
56
+ - lib/logspot.rb
57
+ - logspot.gemspec
58
+ - spec/logspot_spec.rb
59
+ - spec/spec_helper.rb
60
+ homepage: https://github.com/pandora2000/
35
61
  licenses:
36
62
  - MIT
37
63
  metadata: {}