Better Stack Java logging | Better Stack Documentation
Explore documentation
Start logging in 5 minutes
Collect logs from your Java applications using Logback.
1. Dependencies
Add Better Stack Logback appender to your pom.xml or build.gradle file:
Maven (pom.xml) Gradle (build.gradle)
Copied!
You will need a Logback logger with a data-binding package for Jackson.
Don't have Logback in your project yet?
Add these dependencies to your pom.xml or build.gradle file:
Maven (pom.xml) Gradle (build.gradle)
Copied!
2. Setup
Set up log appenders in your logback.xml configuration:
3. Start logging 🎉
Import and use the logger:
Send logs to Logtail
Copied!
You should see your logs in Better Stack → Live tail.
Find structured data under the message_json field.
Better Stack parses any JSON at the end of the log line.
Need help?
Please let us know at hello@betterstack.com.
We're happy to help! 🙏
Additional information
- New to logging in Java? Check out our Intro guide to logging in Java.
- Want to support more data types? Install an add-on module for Jackson and register it in the Logtail appender's
<objectMapperModule>tag. For example, see adding JavaTimeModule on Github. - Need to fine-tune logging? Configure more options inside
<appender name="Logtail">tag inlogback.xml:appName- Application name for Better Stack indexation.sourceToken- Your Better Stack source token.ingestUrl- The ingesting URL of your source. Default:https://in.logs.betterstack.com. Use the ingesting host listed in your source settings.mdcFields- MDC fields that will be sent as metadata, separated by a comma.mdcTypes- MDC fields types that will be sent as metadata, in the same order asmdcFieldsare set up, separated by a comma. Possible values arestring,boolean,int, andlong.maxQueueSize- Maximum number of messages in the queue. Messages over the limit will be dropped. Default: 100000.batchSize- Batch size for the number of messages to be sent via the API. Default: 1000.batchInterval- Maximum wait time for a batch to be sent via the API, in milliseconds. Default: 3000.setConnectTimeout- Connection timeout of the underlying HTTP client, in milliseconds. Default: 5000.readTimeout- Read timeout of the underlying HTTP client, in milliseconds. Default: 10000.maxRetries- Maximum number of retries for sending logs to Better Stack. After that, current batch of logs will be dropped. Default: 5.retrySleepMilliseconds- Number of milliseconds to sleep before retrying to send logs to Better Stack. Default: 300.objectMapperModule- Registers an add-on data type module for Jackson to serialize logged data, e.g.com.fasterxml.jackson.datatype.jsr310.JavaTimeModule. Can be used multiple times.
Example project
Want to try a more detailed example with Maven or Gradle?
See our Java Logtail example projects on GitHub.