Class: Makit::Logging::Sinks::FileSink

Inherits:
Base
  • Object
show all
Defined in:
lib/makit/logging/sinks/file_sink.rb

Overview

File sink for writing log entries to files

This sink is a thin wrapper around UnifiedFileSink that provides a simpler API for single-file logging. It maintains backward compatibility with the original FileSink interface while leveraging the more robust UnifiedFileSink implementation.

Examples:

Basic usage with default JSON formatter

file_sink = FileSink.new(log_file: "makit.log")
logger = Logger.new(sinks: [file_sink])
logger.info("Processing started")

Using built-in formatters

file_sink = FileSink.new(
  log_file: "makit.log",
  formatter: :text
)

Using custom formatter options

file_sink = FileSink.new(
  log_file: "makit.log",
  formatter: :json,
  include_context: true,
  include_metadata: true
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#applicable?, #name

Constructor Details

#initialize(log_file: "makit.log", formatter: :json, append: true, **formatter_options) ⇒ FileSink

Initialize file sink

Parameters:

  • log_file (String) (defaults to: "makit.log")

    path to the log file

  • formatter (Symbol, String) (defaults to: :json)

    formatter format name

  • append (Boolean) (defaults to: true)

    whether to append to existing file (true) or overwrite (false)

  • include_context (Boolean)

    whether to include context in formatted output

  • include_metadata (Boolean)

    whether to include metadata in formatted output

  • show_timestamp (Boolean)

    whether to show timestamp in formatted output

  • show_level (Boolean)

    whether to show log level in formatted output



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/makit/logging/sinks/file_sink.rb', line 52

def initialize(log_file: "makit.log", formatter: :json, append: true, **formatter_options)
  @log_file = log_file
  @formatter = formatter.to_sym
  @append = append

  # Create unified sink configuration

  config = {
    file: @log_file,
    format: @formatter,
    append: @append,
  }.merge(formatter_options)

  @unified_sink = UnifiedFileSink.new(configurations: [config])
end

Instance Attribute Details

#appendBoolean (readonly)

Returns whether to append to existing file (true) or overwrite (false).

Returns:

  • (Boolean)

    whether to append to existing file (true) or overwrite (false)



39
40
41
# File 'lib/makit/logging/sinks/file_sink.rb', line 39

def append
  @append
end

#formatterSymbol (readonly)

Returns the formatter format name.

Returns:

  • (Symbol)

    the formatter format name



37
38
39
# File 'lib/makit/logging/sinks/file_sink.rb', line 37

def formatter
  @formatter
end

#log_fileString (readonly)

Returns path to the log file.

Returns:

  • (String)

    path to the log file



35
36
37
# File 'lib/makit/logging/sinks/file_sink.rb', line 35

def log_file
  @log_file
end

#unified_sinkUnifiedFileSink (readonly)

Returns the underlying unified sink.

Returns:



41
42
43
# File 'lib/makit/logging/sinks/file_sink.rb', line 41

def unified_sink
  @unified_sink
end

Instance Method Details

#call(log_request) {|LogRequest| ... } ⇒ LogRequest

Execute sink logic to write log entry to file

Parameters:

  • log_request (LogRequest)

    the log request to process

Yields:

  • (LogRequest)

    yields the processed log request to the next sink

Yield Returns:

Returns:

  • (LogRequest)

    the final processed log request



73
74
75
# File 'lib/makit/logging/sinks/file_sink.rb', line 73

def call(log_request, &block)
  @unified_sink.call(log_request, &block)
end

#configHash

Get sink configuration

Returns:

  • (Hash)

    sink configuration



80
81
82
83
84
85
86
87
88
# File 'lib/makit/logging/sinks/file_sink.rb', line 80

def config
  {
    name: self.class.name.split("::").last,
    log_file: @log_file,
    formatter: @formatter,
    append: @append,
    unified_config: @unified_sink.config,
  }
end