summaryrefslogtreecommitdiff
path: root/lib/mixlib/log/logger.rb
blob: abc7bc35b07f7fb702ca806374e5d2e25f7756f4 (plain)
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
require "logger"
require "mixlib/log/logging"

# A subclass of Ruby's stdlib Logger with all the mutex and logrotation stuff
# ripped out, and metadata added in.
module Mixlib
  module Log
    class Logger < ::Logger

      include Logging
      #
      # === Synopsis
      #
      #   Logger.new(name, shift_age = 7, shift_size = 1048576)
      #   Logger.new(name, shift_age = 'weekly')
      #
      # === Args
      #
      # +logdev+::
      #   The log device.  This is a filename (String) or IO object (typically
      #   +STDOUT+, +STDERR+, or an open file).
      # +shift_age+::
      #   Number of old log files to keep, *or* frequency of rotation (+daily+,
      #   +weekly+ or +monthly+).
      # +shift_size+::
      #   Maximum logfile size (only applies when +shift_age+ is a number).
      #
      # === Description
      #
      # Create an instance.
      #
      def initialize(logdev)
        @progname = nil
        @level = DEBUG
        @default_formatter = Formatter.new
        @formatter = nil
        @logdev = nil
        if logdev
          @logdev = LocklessLogDevice.new(logdev)
        end
      end

      def add_data(severity, message, progname, data: {})
        return true if @logdev.nil? || severity < @level
        data ||= {}
        if message.kind_of?(::Exception)
          data[:err] = message
        else
          data[:msg] = message
        end
        @logdev.write(
          format_message(format_severity(severity), Time.now, progname, data))
        true
      end
      alias_method :add, :add_data

      class LocklessLogDevice < LogDevice

        def initialize(log = nil)
          @dev = @filename = @shift_age = @shift_size = nil
          if log.respond_to?(:write) && log.respond_to?(:close)
            @dev = log
          else
            @dev = open_logfile(log)
            @filename = log
          end
          @dev.sync = true
        end

        def write(message)
          @dev.write(message)
        rescue Exception => ignored
          warn("log writing failed. #{ignored}")
        end

        def close
          @dev.close rescue nil
        end

        private

        def open_logfile(filename)
          if FileTest.exist?(filename)
            open(filename, (File::WRONLY | File::APPEND))
          else
            create_logfile(filename)
          end
        end

        def create_logfile(filename)
          logdev = open(filename, (File::WRONLY | File::APPEND | File::CREAT))
          add_log_header(logdev)
          logdev
        end

        def add_log_header(file)
          file.write(
            "# Logfile created on %s by %s\n" % [Time.now.to_s, Logger::ProgName]
          )
        end

      end

    end
  end
end