Java Logging to Logfire
Get started with logging from your Java Application using Logback.
Follow these 3 easy steps to setup logging from your Maven or Gradle managed Java project.
1. Add Dependencies
Add Logfire Logback Appenders to your preferred pom.xml
or build.gradle
file:
2. Set up Java logger with Logfire:
Setup log appenders in your logback.xml
configuration, inside resources
directory
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
<appender name="Logfire" class="com.logfire.logback.LogfireAppender">
<appName>MyApp</appName>
<sourceToken>--$SOURCE_TOKEN--</sourceToken>
<mdcFields>requestId,requestTime</mdcFields>
<mdcTypes>string,int</mdcTypes>
<objectMapperModule>com.fasterxml.jackson.datatype.jsr310.JavaTimeModule</objectMapperModule>
</appender>
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="Logfire" />
<appender-ref ref="Console" />
</root>
</configuration>
Replace
$SOURCE_TOKEN
with your source token. You can get your source tokens in your Logfire sources.
3. Start logging π
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class App {
public static void main(String[] args) {
Logger logger = LoggerFactory.getLogger(App.class);
logger.error("An error occurred!!!");
String json = "{ \"item\": \"Superbike\", \"color\": \"pink\" }";
logger.info("Log message with structured logging " + json);
}
}
You should see your logs in Logfire β Live tail.
Additional information
To enhance the clarity and usability of your logging configurations within the logback.xml file for Logfire, adjust the following settings within the
<appender name="Logfire">
tag:appName
- Specify the application name for indexing in Logfire.sourceToken
- Provide your unique Logfire source token.mdcFields
- List the MDC (Mapped Diagnostic Context) fields to be sent as metadata, separated by commas.mdcTypes
- Define the data types for the MDC fields listed above, in the same sequence. Acceptable types include: string, boolean, int, and long.maxQueueSize
- Set the maximum queue capacity for messages. Messages exceeding this limit will be discarded. The default setting is 100,000.batchSize
- Determine the batch size for sending messages through the API. The default setting is 1,000.batchInterval
- Specify the maximum delay, in milliseconds, before a batch is sent via the API. The default setting is 3,000 ms.setConnectTimeout
- Set the connection timeout for the underlying HTTP client, in milliseconds. The default is 5,000 ms.readTimeout
- Establish the read timeout for the underlying HTTP client, in milliseconds. The default is 10,000 ms.maxRetries
- Define the maximum number of retry attempts for sending logs to Logfire. Beyond this limit, the current batch of logs will be discarded. The default setting is 5.retrySleepMilliseconds
- Set the delay, in milliseconds, before retrying to send logs to Logfire. The default is 300 ms.objectMapperModule
- Add a module to the Jackson library to enable serialization of specific data types in your logs, such as com.fasterxml.jackson.datatype.jsr310.JavaTimeModule. This setting can be applied multiple times to include various modules.
Example project
Want to go through a working code with Maven or Gradle? See our Java Logtail example projects on GitHub.