File: logify.rb

package info (click to toggle)
ruby-logify 0.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 128 kB
  • sloc: ruby: 408; makefile: 5
file content (146 lines) | stat: -rw-r--r-- 2,957 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
require 'logify/logger'
require 'logify/version'

module Logify
  # @private
  LEVEL_ID = 'logify.level'

  # @private
  IO_ID = 'logify.io'

  class << self
    # @private
    def included(base)
      base.send(:extend,  ClassMethods)
      base.send(:include, InstanceMethods)
    end

    # @private
    def logger_for(name)
      loggers[name] ||= Logger.new(name)
    end

    #
    # Reset the current loggers for all thread instances.
    #
    # @return [true]
    #
    def reset!
      Thread.list.each do |thread|
        thread[LEVEL_ID] = nil
        thread[IO_ID]    = nil
      end

      loggers.clear
      true
    end

    #
    # The current log level.
    #
    # @return [Fixnum]
    #
    def level
      Thread.current[LEVEL_ID] || Thread.main[LEVEL_ID] || Logger::DEFAULT
    end

    #
    # Set the global log level. All loggers in the current thread will
    # immediately begin using this new log level.
    #
    # @example Setting the log level to +:fatal+
    #   Logify.level = :fatal
    #
    # @param [Symbol] id
    #   the symbol id of the logger
    #
    # @return [Fixnum]
    #
    def level=(id)
      Thread.current[LEVEL_ID] = Logger::LEVEL_MAP.fetch(id, Logger::DEFAULT)
    end

    #
    # The IO stream to log to. Default: +$stdout+.
    #
    # @return [IO]
    #
    def io
      Thread.current[IO_ID] || Thread.main[IO_ID] || $stdout
    end

    #
    # Set the global io object. All loggers in the current thread will
    # immediately begin using this new IO stream. It is the user's
    # responsibility to manage this IO object (like rewinding and closing).
    #
    # @example Setting the outputter to +$stderr+
    #   Logify.io = $stderr
    #
    # @example Using an IO object
    #   io = StringIO.new
    #   Logify.io = io
    #
    # @param [IO] io
    #   the IO object to output to
    #
    # @return [IO]
    #
    def io=(io)
      Thread.current[IO_ID] = io
    end

    #
    # Add a filter parameter to Logify.
    #
    # @example Filter a password in the logger
    #   Logify.filter('P@s$w0r)')
    #   log.debug "This is the P@s$w0r)" #=> "This is the [FILTERED]"
    #
    # @return [void]
    #
    def filter(param)
      filters[param] = nil
    end

    #
    # The list of filters for Logify.
    #
    # @return [Hash]
    #
    def filters
      @filters ||= {}
    end

    private

    # @private
    def loggers
      @loggers ||= {}
    end
  end

  # Class methods that get extended into any including object.
  module ClassMethods
    #
    # Write a message to the logger for this class.
    #
    # @return [Logger]
    #
    def log
      @log ||= Logify.logger_for(name)
    end
  end

  # Instance methods that get included into any including object.
  module InstanceMethods
    #
    # Write a message to the logger for this instance's class.
    #
    # @return [Logger]
    #
    def log
      @log ||= Logify.logger_for(self.class.name)
    end
  end
end