Galileo Context
Manage trace context and control logging behavior with the galileo_context context manager
The galileo_context
context manager provides a convenient way to control the logging behavior of your application. It allows you to:
- Set the project and log stream for all logs within its scope
- Automatically start and flush traces
- Group related operations into a single trace
- Ensure traces are flushed even in long-running applications
Basic Usage
The simplest way to use galileo_context
is as a context manager:
Specifying Project and Log Stream
You can specify the project and log stream to use for all logs within the context:
Using with OpenAI Wrapper
The galileo_context
works seamlessly with the OpenAI wrapper:
Using with Decorated Functions
The galileo_context
also works with functions decorated with @log
:
Initialization and Flushing
In some cases, you may want to initialize the context and flush traces explicitly:
This approach is particularly useful for long-running applications where you need to control when traces are flushed to Galileo.
Nesting Contexts
You can nest galileo_context
calls to temporarily override the project or log stream:
Best Practices
-
Use context managers for logical grouping: Wrap related operations in a
galileo_context
to group them into a single trace. -
Specify project and log stream explicitly: While environment variables work, it’s often clearer to specify the project and log stream in your code.
-
Flush in long-running applications: In applications that don’t terminate (like web servers or Streamlit apps), explicitly flush traces to ensure they’re sent to Galileo.
-
Use nesting judiciously: Nested contexts can be powerful but can also make your code harder to follow. Use them when you need to temporarily override settings.
Related Resources
- @log Decorator - For decorating functions with logging
- GalileoLogger - For more manual control over logging
- OpenAI Wrapper - For automatic logging of OpenAI calls