Class: Langchain::ContextualLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/langchain/contextual_logger.rb

Constant Summary collapse

MESSAGE_COLOR_OPTIONS =
{
  debug: {
    color: :white
  },
  error: {
    color: :red
  },
  fatal: {
    color: :red,
    background: :white,
    mode: :bold
  },
  unknown: {
    color: :white
  },
  info: {
    color: :white
  },
  warn: {
    color: :yellow,
    mode: :bold
  }
}

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ ContextualLogger

Returns a new instance of ContextualLogger.



29
30
31
32
# File 'lib/langchain/contextual_logger.rb', line 29

def initialize(logger)
  @logger = logger
  @levels = Logger::Severity.constants.map(&:downcase)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, **kwargs, &block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/langchain/contextual_logger.rb', line 38

def method_missing(method, *args, **kwargs, &block)
  return @logger.send(method, *args, **kwargs, &block) unless @levels.include?(method)

  for_class = kwargs.delete(:for)
  for_class_name = for_class&.name

  log_line_parts = []
  log_line_parts << "[Langchain.rb]".colorize(color: :yellow)
  log_line_parts << if for_class.respond_to?(:logger_options)
    "[#{for_class_name}]".colorize(for_class.logger_options) + ":"
  elsif for_class_name
    "[#{for_class_name}]:"
  end
  log_line_parts << args.first.colorize(MESSAGE_COLOR_OPTIONS[method])
  log_line = log_line_parts.compact.join(" ")

  @logger.send(
    method,
    log_line
  )
end

Instance Method Details

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/langchain/contextual_logger.rb', line 34

def respond_to_missing?(method, include_private = false)
  @logger.respond_to?(method, include_private)
end