Class: LLM::Tracer::PrettyLogger

Inherits:
Tracer
  • Object
show all
Defined in:
lib/llm/tracer/pretty_logger.rb

Overview

The LLM::Tracer::PrettyLogger class writes human-readable request and tool call logs to a console or file. Each event is a single line with the relevant context inline, no structured JSON.

Examples:

llm = LLM.openai(key: ENV["KEY"])
llm.tracer = LLM::Tracer.pretty_logger(llm)

Writing to a file

llm.tracer = LLM::Tracer.pretty_logger(llm, path: "log.txt")

Instance Method Summary collapse

Constructor Details

#initialize(llm, options = {}) ⇒ PrettyLogger

Returns a new instance of PrettyLogger.

Parameters:

  • llm (LLM::Provider) —

    A provider

  • options (Hash) (defaults to: {}) —

    A hash of options



19
20
21
22
# File 'lib/llm/tracer/pretty_logger.rb', line 19

def initialize(llm, options = {})
  super
  setup!(**options)
end

Instance Method Details

#on_exit ⇒ nil

No-op.

Returns:

  • (nil)


93
94
95
# File 'lib/llm/tracer/pretty_logger.rb', line 93

def on_exit
  nil
end

#on_request_start(operation:, model: nil)

This method returns an undefined value.

Parameters:

  • operation (String)
  • model (String) (defaults to: nil)
  • inputs (Hash, nil) —

    Optional span attributes (e.g. gen_ai.input.messages) from llm.rb or caller.

  • request_id (String) —

    The id of the request. It is a UUIDv7, and it is shared by #on_request_start, #on_request_finish, and #on_request_error for the same request.



27
28
29
30
31
# File 'lib/llm/tracer/pretty_logger.rb', line 27

def on_request_start(operation:, model: nil, **)
  @start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  name = operation == "chat" ? "chat" : operation
  @io.puts "#{timestamp} #{llm.name} #{name} (#{model || "default"})"
end

#on_request_finish(operation:, res:, model: nil)

This method returns an undefined value.

Parameters:

  • operation (String)
  • res (LLM::Response)
  • span (Object, nil)
  • model (String) (defaults to: nil)
  • outputs (Hash, nil) —

    Optional span attributes (e.g. gen_ai.output.messages) from llm.rb or caller.

  • metadata (Hash, nil) —

    Optional metadata from llm.rb or caller.

  • request_id (String) —

    The id of the request that can be matched back to #on_request_start



36
37
38
39
40
41
42
43
44
# File 'lib/llm/tracer/pretty_logger.rb', line 36

def on_request_finish(operation:, res:, model: nil, **)
  elapsed = @start ? (Process.clock_gettime(Process::CLOCK_MONOTONIC) - @start).round(2) : nil
  tokens = format_tokens(res)
  name = operation == "chat" ? "chat" : operation
  parts = ["#{timestamp} #{llm.name} #{name} done"]
  parts << tokens if tokens
  parts << "#{elapsed}s" if elapsed
  @io.puts parts.join(", ")
end

#on_request_error(ex:)

This method returns an undefined value.

Parameters:



49
50
51
# File 'lib/llm/tracer/pretty_logger.rb', line 49

def on_request_error(ex:, **)
  @io.puts "#{timestamp} #{llm.name} error #{ex.class}: #{ex.message}"
end

#on_tool_start(id:, name:, arguments:) ⇒ LLM::Object

Returns The span an ending is handed, with the call's id and name.

Parameters:

  • id (String) —

    The tool call ID assigned by the model/provider

  • name (String) —

    The tool (function) name.

  • arguments (Hash) —

    The parsed tool arguments.

  • model (String) —

    The model name

Returns:

  • (LLM::Object) —

    The span an ending is handed, with the call's id and name



57
58
59
60
# File 'lib/llm/tracer/pretty_logger.rb', line 57

def on_tool_start(id:, name:, arguments:, **)
  @io.puts "#{timestamp} #{name}(#{format_arguments(arguments)})"
  LLM::Object.from(id:, name:)
end

#on_tool_finish(result:)

This method returns an undefined value.

Parameters:



65
66
67
# File 'lib/llm/tracer/pretty_logger.rb', line 65

def on_tool_finish(result:, **)
  @io.puts "#{timestamp} #{result.name} -> #{format_value(result.value)}"
end

#on_tool_error(ex:)

This method returns an undefined value.

Parameters:



72
73
74
# File 'lib/llm/tracer/pretty_logger.rb', line 72

def on_tool_error(ex:, **)
  @io.puts "#{timestamp} tool error #{ex.value[:type]}: #{ex.value[:message]}"
end

#on_tool_interrupt(ex:, span:)

This method returns an undefined value.

Parameters:



79
80
81
# File 'lib/llm/tracer/pretty_logger.rb', line 79

def on_tool_interrupt(ex:, span:, **)
  @io.puts "#{timestamp} tool #{span.name} (#{format_id(span.id)}) received an interrupt"
end

#on_interrupt(scope:)

This method returns an undefined value.

Parameters:



86
87
88
# File 'lib/llm/tracer/pretty_logger.rb', line 86

def on_interrupt(scope:, **)
  @io.puts "#{timestamp} #{llm.name} interrupt received (scope=#{scope})"
end