If you aren’t already collecting logs with Datadog, see the Logs documentation to set up logs. Ensure that the source tag (specifying language) is properly configured. Datadog recommends setting up Agent-based log collection.
For languages such as Python, Java, and Ruby, no additional configuration is needed if the source tag in your logs is configured correctly. All required attributes are automatically tagged and sent to Datadog.
For backend languages such as C#, .NET, Go, and Node.js, the code examples in each section demonstrate how to properly configure an error log and attach the required stack trace in the log’s error.stack.
If you are already sending stack traces to Datadog but they are not in error.stack, you can set up a generic log remapper to remap the stack trace to the correct attribute in Datadog.
To configure inline code snippets in issues, set up the source code integration. Adding code snippets in Error Tracking for Logs does not require APM; the enrichment tags and linked repository is the same for both.
To enable Error Tracking, logs must include the following attributes:
Either an error.kind or error.stack field. Note: if using error.stack, it must be a valid stack trace.
A Service attribute
A status level of ERROR, CRITICAL, ALERT, or EMERGENCY.
The remaining attributes listed below are optional, but their presence improves error grouping.
Specific attributes have a dedicated UI display within Datadog. To enable these functionalities for Error Tracking, use the following attribute names:
Attribute
Description
error.stack
Actual stack trace
error.message
Error message contained in the stack trace
error.kind
The type or “kind” of an error (for example, “Exception”, or “OSError”)
Note: By default, integration Pipelines attempt to remap default logging library parameters to those specific attributes and parse stack traces or traceback to automatically extract the error.message and error.kind.
To log a caught exception yourself, you may optionally use:
varlog=newLoggerConfiguration().WriteTo.File(newJsonFormatter(renderMessage:true),"log.json").Enrich.WithExceptionDetails().CreateLogger();try{// …}catch(Exceptionex){// pass exception as first argument of log calllog.Error(ex,"an exception occurred");}
To log a caught exception yourself, you may optionally use:
privatestaticLoggerlog=LogManager.GetCurrentClassLogger();staticvoidMain(string[]args){try{// …}catch(Exceptionex){// pass exception as second argument of log calllog.ErrorException("an exception occurred",ex);}}
To log a caught exception yourself, you may optionally use:
classProgram{privatestaticILoglogger=LogManager.GetLogger(typeof(Program));staticvoidMain(string[]args){try{// …}catch(Exceptionex){// pass exception as second argument of log calllog.Error("an exception occurred",ex);}}}
To log a caught exception yourself, you may optionally use:
Loggerlogger=LogManager.getLogger("HelloWorld");try{// …}catch(Exceptione){// pass exception as last argument of log calllogger.error("an exception occurred",e)}
To log a caught exception yourself, you may optionally use:
Loggerlogger=LoggerFactory.getLogger(NameOfTheClass.class);try{// …}catch(Exceptione){// pass exception as last argument of log calllogger.error("an exception occurred",e)}
If you use Lograge, you can also set it up to send formatted error logs:
Rails.application.configuredojsonLogger=Logger.new(STDOUT)# STDOUT or file depending on your agent configurationjsonLogger.formatter=JsonWithErrorFieldFormatter.new# Replacing Rails default TaggedLogging logger with a new one with the json formatter.# TaggedLogging is incompatible with more complex json format messagesconfig.logger=jsonLogger# Lograge configconfig.lograge.enabled=trueconfig.lograge.formatter=Lograge::Formatters::Raw.new# Disables log colorationconfig.colorize_logging=false# Configure logging of exceptions to the correct fieldsconfig.lograge.custom_options=lambdado|event|ifevent.payload[:exception_object]return{level:'ERROR',message:event.payload[:exception_object].inspect,error:{kind:event.payload[:exception_object].class,message:event.payload[:exception_object].message,stack:event.payload[:exception_object].backtrace.join("\n")}}endendend