Class: CemAcpt::Logging::Logger

Inherits:
Logger
  • Object
show all
Defined in:
lib/cem_acpt/logging.rb

Overview

Delagator class for the standard Ruby Logger class. This class is used to ensure the Github Actions formatter correctly formats the log output when using the standard Ruby Logger class methods such as #info, #debug, etc.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logdev, shift_age = 0, shift_size = 1_048_576, **kwargs) ⇒ Logger

Returns a new instance of Logger.



74
75
76
77
78
79
80
81
82
# File 'lib/cem_acpt/logging.rb', line 74

def initialize(logdev, shift_age = 0, shift_size = 1_048_576, **kwargs)
  @config_hash = { logdev: logdev, shift_age: shift_age, shift_size: shift_size, **kwargs }
  @ci_mode = (kwargs[:formatter]&.downcase&.to_sym == :github_action || !ENV['GITHUB_ACTIONS'].nil? || !ENV['CI'].nil?)
  kwargs[:formatter] = CemAcpt::Logging::Formatter.for(kwargs[:formatter]) if kwargs[:formatter]
  super(logdev, shift_age, shift_size, **kwargs)
  @raw_logdev = logdev # Used for logging from trap context
  @trap_context = false # If true, will not use the standard Logger methods and will write directly to the logdev
  @verbose = false
end

Instance Attribute Details

#config_hashObject (readonly)

Returns the value of attribute config_hash.



71
72
73
# File 'lib/cem_acpt/logging.rb', line 71

def config_hash
  @config_hash
end

#trap_contextObject

Returns the value of attribute trap_context.



72
73
74
# File 'lib/cem_acpt/logging.rb', line 72

def trap_context
  @trap_context
end

Instance Method Details

#debug(progname = nil, &block) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/cem_acpt/logging.rb', line 100

def debug(progname = nil, &block)
  severity = 'debug'
  return log_trap_context(severity, progname, &block) if log_trap_context?(severity)
  return log_ci_mode(severity, progname, &block) if log_ci_mode?(severity)

  super
end

#end_ci_groupObject



148
149
150
# File 'lib/cem_acpt/logging.rb', line 148

def end_ci_group
  self.<< "::endgroup::\n" if @ci_mode
end

#error(progname = nil, &block) ⇒ Object



124
125
126
127
128
129
130
# File 'lib/cem_acpt/logging.rb', line 124

def error(progname = nil, &block)
  severity = 'error'
  return log_trap_context(severity, progname, &block) if log_trap_context?(severity)
  return log_ci_mode(severity, progname, &block) if log_ci_mode?(severity)

  super
end

#fatal(progname = nil, &block) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/cem_acpt/logging.rb', line 132

def fatal(progname = nil, &block)
  severity = 'fatal'
  return log_trap_context(severity, progname, &block) if log_trap_context?(severity)
  return log_ci_mode(severity, progname, &block) if log_ci_mode?(severity)

  super
end

#info(progname = nil, &block) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/cem_acpt/logging.rb', line 108

def info(progname = nil, &block)
  severity = 'info'
  return log_trap_context(severity, progname, &block) if log_trap_context?(severity)
  return log_ci_mode(severity, progname, &block) if log_ci_mode?(severity)

  super
end

#set_verbose(value) ⇒ Object

Raises:

  • (ArgumentError)


84
85
86
87
88
# File 'lib/cem_acpt/logging.rb', line 84

def set_verbose(value)
  raise ArgumentError, 'value must be a boolean' unless [true, false].include?(value)

  @verbose = value
end

#start_ci_group(name) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/cem_acpt/logging.rb', line 140

def start_ci_group(name)
  if @ci_mode
    self.<< "::group::#{name}\n"
  else
    info(name)
  end
end

#verbose(progname = nil, &block) ⇒ Object



90
91
92
93
94
# File 'lib/cem_acpt/logging.rb', line 90

def verbose(progname = nil, &block)
  return unless @verbose

  debug(progname, &block)
end

#verbose?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/cem_acpt/logging.rb', line 96

def verbose?
  @verbose
end

#warn(progname = nil, &block) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/cem_acpt/logging.rb', line 116

def warn(progname = nil, &block)
  severity = 'warn'
  return log_trap_context(severity, progname, &block) if log_trap_context?(severity)
  return log_ci_mode(severity, progname, &block) if log_ci_mode?(severity)

  super
end