noddy 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -6,8 +6,7 @@ This is very much a work in progress, but is still usable.
6
6
 
7
7
  TODO:
8
8
 
9
- - Specify log output e.g. STDERR, STDOUT, File
10
- - Sepcifiy log format
9
+ - Sepcifiy log format (text, json etc)
11
10
 
12
11
  ## Installation
13
12
 
@@ -31,6 +30,8 @@ require "noddy"
31
30
  Noddy.log_level = Noddy::DEBUG # Default: Noddy::INFO
32
31
  Noddy.colour = true # Default: false
33
32
  Noddy.timestamp = false # Default: true
33
+ Noddy.outputs = [STDOUT, Noddy::FILE] # Default [STDERR]
34
+ Noddy.outputs = [STDOUT, '/var/log/dca/test.log'] # Using a custom log path
34
35
 
35
36
  Noddy.debug "I am just a debug message"
36
37
  Noddy.info "I am an info message"
data/lib/noddy.rb CHANGED
@@ -4,10 +4,6 @@ require "noddy/string"
4
4
 
5
5
  module Noddy
6
6
  class << self
7
- def log_level=( level )
8
- @level = level
9
- end
10
-
11
7
  def debug( msg )
12
8
  log_message( msg, DEBUG )
13
9
  end
@@ -29,6 +25,10 @@ module Noddy
29
25
  end
30
26
 
31
27
 
28
+ def log_level=( level )
29
+ @level = level
30
+ end
31
+
32
32
  def colour=( colour )
33
33
  @colour = colour
34
34
  end
@@ -37,14 +37,52 @@ module Noddy
37
37
  @timestamp = timestamp
38
38
  end
39
39
 
40
+ def outputs=( outputs = [] )
41
+ @outputs = outputs
42
+ end
43
+
40
44
  private
41
45
  def log_message( msg, level )
42
-
43
- msg.colour!( level ) if @colour || COLOUR_DEFAULT
46
+ outputs = @outputs || OUTPUTS_DEFAULT
44
47
 
45
48
  msg.timestamp! if @timestamp || TIMESTAMP_DEFAULT
46
49
 
47
- puts msg if level <= (@level || DEFAULT_LEVEL )
50
+ outputs.each do |output|
51
+ case output
52
+ when STDOUT
53
+ if @colour || COLOUR_DEFAULT
54
+ STDOUT.puts msg.colour( level ) if level <= (@level || DEFAULT_LEVEL )
55
+ else
56
+ STDOUT.puts msg if level <= (@level || DEFAULT_LEVEL )
57
+ end
58
+ when STDERR
59
+ if @colour || COLOUR_DEFAULT
60
+ STDERR.puts msg.colour( level ) if level <= (@level || DEFAULT_LEVEL )
61
+ else
62
+ STDERR.puts msg if level <= (@level || DEFAULT_LEVEL )
63
+ end
64
+ when String
65
+ write_log_file( output, msg, level )
66
+ else
67
+ raise "Unknow output type of class #{output.class}"
68
+ end
69
+ end
48
70
  end
71
+
72
+ def write_log_file( output, msg, level )
73
+ log_dir = File.dirname output
74
+ raise "Log file directory #{log_dir} does not exist" unless Dir.exist? log_dir
75
+
76
+ begin
77
+ File.open(output, 'a', 0644) do |f|
78
+ f.flock(File::LOCK_EX)
79
+ f.write msg + "\n"
80
+ f.flush
81
+ end
82
+ rescue Errno::EACCES => e
83
+ raise "Permissioned denided writing to #{output}"
84
+ end
85
+ end
86
+
49
87
  end
50
88
  end
@@ -12,4 +12,8 @@ module Noddy
12
12
 
13
13
  TIMESTAMP_DEFAULT = true
14
14
 
15
+ OUTPUTS_DEFAULT = [STDERR]
16
+
17
+ FILE = '/var/log/' + File.basename($0).gsub(/\.\w+$/, '') + '.log'
18
+
15
19
  end
data/lib/noddy/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Noddy
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/noddy.gemspec CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Dave Avent"]
10
10
  spec.email = ["davent@lumux.co.uk"]
11
11
  spec.summary = %q{Noddy Logger logging library.}
12
- spec.description = %q{Handles logging to stdout, stderr and file as well as log levels, timestamping and compression.}
12
+ spec.description = %q{Noddy Logger is a very simple library to quickly output logs from your scripts and applications using colours, timestamps etc.}
13
13
  spec.homepage = "https://github.com/davent/noddy"
14
14
  spec.license = "MIT"
15
15
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: noddy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-07-11 00:00:00.000000000 Z
12
+ date: 2014-07-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -43,8 +43,8 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
- description: Handles logging to stdout, stderr and file as well as log levels, timestamping
47
- and compression.
46
+ description: Noddy Logger is a very simple library to quickly output logs from your
47
+ scripts and applications using colours, timestamps etc.
48
48
  email:
49
49
  - davent@lumux.co.uk
50
50
  executables: []