Class: Log

Inherits:
Object
  • Object
show all
Defined in:
lib/AuthenticationSDK/logging/log_factory.rb

Overview

Logger Class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logConfiguration, callingProgram = '') ⇒ Log

Returns a new instance of Log.



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
# File 'lib/AuthenticationSDK/logging/log_factory.rb', line 10

def initialize(logConfiguration, callingProgram = '')
    enableLog = logConfiguration.enableLog
    loggingLevel = logConfiguration.loggingLevel
    logDirectory = logConfiguration.logDirectory
    logFilename = logConfiguration.logFilename
    maxLogSize = logConfiguration.maxLogSize
    maxLogFiles = logConfiguration.maxLogFiles
    enableMasking = logConfiguration.enableMasking

    callingProgram = "Generic" if callingProgram.nil? || callingProgram.empty?

    #log
    if enableLog == true
        filename = File.join logDirectory, logFilename + '.log' # logDirectory + '/' + logFilename + '.log'
        datetime = DateTime.now
        if File.exist?(logDirectory)
            if File.exist?(logFilename)
                if File.size(filename) >= maxLogSize
                    updatedFileName = File.join logDirectory, logFilename + '_' + datetime.strftime('%Y%m%d%H%M%S') + '.log'
                    File.rename(filename, updatedFileName)
                end
            end
        elsif !File.exist?(logDirectory)
            FileUtils.mkdir_p logDirectory
            File.new(filename, "w")
        end

        STDOUT.sync = true

        @logger = Logger.new(STDOUT, progname: callingProgram)
        @logger = Logger.new(filename, shift_age = maxLogFiles, shift_size = maxLogSize, progname: callingProgram)
        @logger.level = loggingLevel
        @logger.datetime_format = datetime.strftime('%Y-%m-%d %H:%M:%S')

        if enableMasking
            @logger.formatter = SensitiveDataFilter.new
        else
            @logger.formatter = proc do |severity, datetime, progname, msg|
                date_format = datetime.strftime("%Y-%m-%d %H:%M:%S")
                log_message = "[#{date_format}] #{severity.ljust(10)} (#{progname}): #{msg}\n"
                # $stdout.puts log_message
                log_message
            end
        end
    else
        @logger = Logger.new(false)
    end
rescue StandardError => err
    # puts err
    raise err
    # exit!
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



63
64
65
# File 'lib/AuthenticationSDK/logging/log_factory.rb', line 63

def logger
  @logger
end