Skip to main content
The GalileoLogger class provides the most granular control over logging in Galileo. You can create a logger yourself, or use one from the current context from inside a decorated or wrapped function or when using a third-party SDK integration.

Overview

The GalileoLogger class allows you to:
  • Start sessions
  • Manually create and manage traces
  • Add spans of different types to your traces
  • Control exactly what data gets logged
  • Explicitly manage when traces are flushed to Galileo
This approach requires more code than using wrappers or decorators but gives you the most control over the logging process.

Python SDK reference

The full SDK reference for the GalileoLogger Python class.

TypeScript SDK reference

The full SDK reference for the GalileoLogger TypeScript class.

Environment variables

GalileoLogger loads configurations from environment variables:
  • Galileo API key in GALILEO_API_KEY
  • Project to log to:
    • Project name in GALILEO_PROJECT (most common usage)
    • Project ID in GALILEO_PROJECT_ID (alternative usage)
  • Log stream to log to in GALILEO_LOG_STREAM
The project name, project ID, or Log stream name can also be set in code (the GalileoLogger SDK reference has more info). A example .env file is available in this Python SDK repository location.

Basic usage

Here’s a simple example of using the GalileoLogger to log an LLM call. The full Python code is available as basic-example.py in this folder.
This example:
  1. Starts a new session
  2. Starts a trace inside the session
  3. Adds an LLM span to the trace
  4. Concludes the trace
  5. Flushes the logger to send the session to Galileo

Detailed API

Initialization

See the GalileoLogger Python SDK docs or TypeScript SDK docs for more details.

Get the current logger from the current context

The Galileo context management keeps track of loggers. You can get the current logger, which will create a new one if there isn’t an existing logger.
If you are using any of the decorators, wrappers, or third-party integrations then this allows you to get the logger created by those components. For example, if you are adding a call inside a method decorated by the Python @log decorator, wrapped in the TypeScript log wrapper, or created automatically by an experiment, then this will return that logger instance so you can manually add additional spans.
See the galileo_context Python SDK docs or getLogger TypeScript SDK docs for more details.

Manage sessions

All traces live inside a session. If you don’t create a session, then one is created automatically with a generated name.

Start a session

You can start a new session, providing a name and an external ID.
The name and external ID fields are optional. If you want to connect a session to another ID that you are using internally, for example a unique ID for a chatbot conversation, then you can set this in the external ID field.

Add metadata to a session

You can attach metadata to a session when starting it. Metadata is a dictionary of string key-value pairs that can be used to add structured information to your session, such as customer IDs, environment names, or application versions.
For more information on using tags and metadata, see Tags and Metadata. See the start_session Python SDK docs or startSession TypeScript SDK docs for more details on all available parameters.

Continue an existing session

If you want to add a trace to an existing session, you can set the current session for the logger, passing the session ID. This is useful if you want to persist a session, for example saving a chatbot conversation with a user mid conversation, then resuming the next time a user connects.
See the set_session Python SDK docs or setSession TypeScript SDK docs for more details. You can also continue a conversation using an external ID using the start session function.

End a session

To stop logging to a session, you can clear the current session.
See the clear_session Python SDK docs or clearSession TypeScript SDK docs for more details.

Start a trace

Once a trace is started, all spans added to that logger will be added to that trace.
See the start_trace Python SDK docs or startTrace TypeScript SDK docs for more details.

Add spans

The GalileoLogger supports adding different types of spans to your traces. All spans take the input and output, as well as a name, duration, tags, and other metadata.

Agent spans

Agent spans are for logging the input and output to agents of different types. The type of agent can be set when creating the span, such as supervisor or planner.
See the add_agent_span Python SDK docs or addAgentSpan TypeScript SDK docs for more details.

LLM spans

LLM spans are for logging calls to LLMs. You can log the input and output, tools, and details like input and output tokens.
See the add_llm_span Python SDK docs or addLlmSpan TypeScript SDK docs for more details.

Retriever spans

Retriever spans are for logging calls to RAG systems. You can log the output from the RAG system to evaluate metrics like Context Adherence.
See the add_retriever_span Python SDK docs or addRetrieverSpan TypeScript SDK docs for more details.

Tool spans

Tool spans log calls to tools, including tools exposed by MCP servers.
See the add_tool_span Python SDK docs or addToolSpan TypeScript SDK docs for more details.

Workflow spans

Workflow spans allow you to group spans into separate workflows for easier monitoring.
Workflow spans are created as child spans of the current trace, or the current workflow span if one was created already and not concluded. Once you create a workflow span, all subsequent spans are created as children of that workflow span. To end a workflow span, call conclude on the logger. The output passed to conclude will be set as the output of the workflow span. Once the workflow span is concluded, any newly added spans will be created on that workflow spans parent span or trace. See the add_workflow_span Python SDK docs or addWorkflowSpan TypeScript SDK docs for more details.

Conclude

When you have finished logging a trace, you can conclude it with the final output. This ends the trace, and a new trace needs to be created to continue logging. The wrappers and third-party integrations will conclude traces for you.
See the conclude Python SDK docs or conclude TypeScript SDK docs for more details.

Flush

Logs are not continuously sent to Galileo to help your application stay performant. You can flush logs when you are ready. The wrappers and third-party integrations will flush logs for you at the end of each trace.
See the flush Python SDK docs or flush TypeScript SDK docs for more details. The flush call on the logger will just flush that specific logger. To flush all loggers, you can flush at the context level.

Advanced usage

Create a single LLM span trace

For simple LLM calls, you can create a trace with a single LLM span in one step:
See the add_single_llm_span_trace Python SDK docs or addSingleLlmSpanTrace TypeScript SDK docs for more details.

Complex trace example

Here’s an example of creating a more complex trace with multiple spans. The full Python code is available as retriever-example.py in this folder.

Best practices

  1. Use higher-level abstractions when possible: The @log decorator and wrappers are easier to use and less error-prone.
  2. Flush traces when appropriate: Call flush() at the end of a request or user interaction to ensure data is sent to Galileo.
  3. Include relevant metadata: Add tags and metadata to make it easier to filter and analyze your traces.
  4. Structure spans logically: Create a span hierarchy that reflects the logical structure of your application.
  5. Handle errors gracefully: Include status codes and error information in your spans to help with debugging.

Next steps

Basic logging components

Log decorator

Quickly add logging to your code with the log decorator and wrapper.

Galileo context

Manage logging using the Galileo context manager.

Integrations with third-party SDKs

OpenAI wrapper

Automatically log calls to the OpenAI SDK with a wrapper.

OpenAI Agents trace processor

Automatically log all the steps in your OpenAI Agent SDK apps using the Galileo trace processor.

LangChain callback

Automatically log all the steps in your LangChain or LangGraph application with the Galileo callback.