Extra reading
logger.ts
This file provides the Logger
class used throughout the Daydreams framework
for recording informational messages, warnings, and errors that occur during
agent execution. It helps you understand what your agent is doing and diagnose
problems.
How to Use
You don't typically create a Logger
instance yourself. The agent object
returned by createDreams
already has a pre-configured logger available at
agent.logger
. You use this instance inside your action
handlers, context
lifecycle methods, or service
definitions to log relevant information:
- Common methods are
agent.logger.info()
,agent.logger.warn()
,agent.logger.error()
,agent.logger.debug()
, andagent.logger.trace()
. - Each method takes a
context
string (often the function/component name), amessage
string, and optionaldata
object.
Benefit
Provides a standard way to record what's happening inside your agent. This is crucial for:
- Debugging: Seeing the flow of execution, variable values, and errors.
- Monitoring: Understanding how your agent is performing in production.
- Auditing: Keeping a record of important events or decisions. The default logger prints messages to the console with timestamps, levels, and context, making it easy to follow along.
Anticipated Questions
- "How can I change the logging level (e.g., see DEBUG messages)?" You can set
the
logLevel
option when callingcreateDreams
. For example:createDreams({ ..., logLevel: LogLevel.DEBUG })
. The levels areERROR
,WARN
,INFO
,DEBUG
,TRACE
(most verbose). - "Can I send logs somewhere other than the console?" Yes, the logger is
designed with "transports". While the default is
ConsoleTransport
, you could potentially implement custom transports (though this is an advanced topic not covered here) and provide them via thelogger
ortransports
option increateDreams
. - "Why provide a
context
string (like'processData:handler'
)?" It helps identify where in the code the log message originated, which is very useful for debugging complex agents.