Class: Pantheios::Services::SimpleFileLogService

Inherits:
Object
  • Object
show all
Defined in:
lib/pantheios/services/simple_file_log_service.rb

Overview

A class that fulfils the Pantheios LogService protocol that allows all severities and logs to a file

NOTE: The LogService protocol is implemented by a class that provides the instance methods severity_logged?(severity : Object) : boolean and log(severity : Object, t : Time, prefix : String|Array, msg : String)

Defined Under Namespace

Modules: SimpleFileLogService_Constants

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log_file_or_path, **options) ⇒ SimpleFileLogService

Signature

  • Parameters:

    • log_file_or_path
      ::File, ::IO, ::String

      A file or an

      IO that will be used as the target of the log statements, or a string specifying the path of the file to be used as the target

    • options
      ::Hash

      Options. Options other than those listed

      are ignored silently (except if $DEBUG, in which case a warning will be issued)

  • Options:

    • :roll_period

      ( :daily, :weekly, :monthly ) The

      periodicity of the log-file rolling. Ignored unless log_file_or_path is a ::String. Ignored if either :roll_size or :roll_depth is specified

    • :roll_size
      ::Integer, [ ::Integer, ::Integer

      ] An integer

      specifying the size of the log file, or an array containing the size of the log file and the depth of the log roll

    • :roll_depth
      ::Integer

      The depth of the size-based log

      rolling. Overrides the second element in an array specified for :roll_size

Raises:

  • (ArgumentError)


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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/pantheios/services/simple_file_log_service.rb', line 98

def initialize log_file_or_path, **options

  roll_period  =  options[:roll_period]
  roll_size  =  options[:roll_size]
  roll_depth = options[:roll_depth]

  if $DEBUG

    options.each do |k, v|

      warn "#{self.class}##{__method__}(): ignoring unrecognised option '#{k}'" unless SimpleFileLogService_Constants::RECOGNISED_OPTIONS.include?(:k)
    end
  end

  if roll_period && (roll_size || roll_depth)

    warn "#{self.class}##{__method__}(): caller specified :roll_depth/:roll_period with :roll_size to #{self.class}##{__method__}() - ignoring :roll_period" if $DEBUG

    roll_period = nil
  end

  if roll_size || roll_depth

    if ::Array === roll_size

      roll_size, d = roll_size

      roll_depth   ||=  d
    end

    roll_size ||= SimpleFileLogService_Constants::DEFAULT_ROLL_SIZE
    roll_depth  ||=  SimpleFileLogService_Constants::DEFAULT_ROLL_DEPTH
  end

  logger_init_args = []
  logger_init_options  =  {}

  if false;
  elsif roll_depth

    logger_init_args  << roll_depth
    logger_init_args  << roll_size
  elsif roll_period

    roll_period = roll_period.downcase.to_sym if ::String === roll_period

    case roll_period
    when :daily, :weekly, :monthly

      ;
    else

      raise ArgumentError, ":roll_period value must be one of :daily, :weekly, :monthly"
    end

    logger_init_args  << roll_period.to_s
    logger_init_args  << 0
  end

  raise ArgumentError, ":roll_depth must be a non-negative integer" unless roll_depth.nil? || (::Integer === roll_depth && roll_depth >= 0)
  raise ArgumentError, ":roll_size must be a non-negative integer" unless roll_size.nil? || (::Integer === roll_size && roll_size >= 0)

  file_proc = lambda do

    @logger     = ::Logger.new log_file_or_path, *logger_init_args
    @log_file_path  =  log_file_or_path.path
  end

  io_proc = lambda do

    @logger     = ::Logger.new log_file_or_path, *logger_init_args
    @log_file_path  =  log_file_or_path.respond_to?(:path) ? log_file_or_path.path : nil
  end

  case log_file_or_path
  when nil

    raise ArgumentError, 'log_file_or_path may not be nil'
  when ::IO#, ::StringIO

    io_proc.call
  when ::File#, ::Tempfile

    file_proc.call
  when ::String

    @logger     = ::Logger.new log_file_or_path, *logger_init_args
    @log_file_path  =  log_file_or_path
  else

    ancestor_names  =  log_file_or_path.class.ancestors.map(&:to_s)

    if false

      ;
    elsif ancestor_names.include?('StringIO')

      io_proc.call
    elsif ancestor_names.include?('Tempfile')

      file_proc.call
    else

      raise TypeError, "log_file_or_path type must be one of #{::File}, #{::IO}, #{::String}, #{::StringIO}"
    end
  end

  self
end

Instance Attribute Details

#log_file_pathObject (readonly)

The path of the underlying log file. May return nil if the path cannot be determined



210
211
212
# File 'lib/pantheios/services/simple_file_log_service.rb', line 210

def log_file_path
  @log_file_path
end

Instance Method Details

#log(sev, t, pref, msg) ⇒ Object



217
218
219
220
# File 'lib/pantheios/services/simple_file_log_service.rb', line 217

def log sev, t, pref, msg

  @logger << "#{pref}#{msg}\n"
end

#severity_logged?(severity) ⇒ Boolean

Returns:

  • (Boolean)


212
213
214
215
# File 'lib/pantheios/services/simple_file_log_service.rb', line 212

def severity_logged? severity

  true
end