Get Started
Quick Start
Configurate logger for formater and sanitizer, Configurate app-node for tracing. Write logs through annotation @Logging or traditional logger.log().
1. Configuration
bitryon:
logger:
file-name: ./logs/${spring.application.name} # will be like ./logs/bitryon-logging-java-spring-example.20250415-161023-565.0.log
file-max-length: 536870912
print-stdout: true # print to console
print-file: true # write to file
log-date-time-format: yyyy-MM-dd HH:mm:ss.SSS # UTC. the time on the head of the log. Leave blank will print System.currentTimeMillis
log-async: true # false blocking IO; true NIO write files, it could lose part of logs if JVM crashes
log-light: false # full step info, or light. true: MedicController.java#io.bitryon.web.controller.MedicController#query#123; no thread info, type JSON->J, false: #.MedicController#query#123 [the sanitizer-patterns may be slightly different].
log-pretty: true # print pretty json and log, by new line and intends. close it in prd if needed.
log-sanitizer-patterns: >
/JSON/*Service*.java*.service.*UserService*#*/MTIzNDU2NzgxMjM0NTY3ODEyMzQ1Njc4/AES(driverLisenceId)/*/MASK$2(name)//SKIP(birthday),
/JSON/*MedicService*#*/MTIzNDU2NzgxMjM0NTY3ODEyMzQ1Njc4/AES(driverLisenceId)/*/MASK$2(name)//SKIP(birthday),
/HTTP/*LoggingHttpRequestWebReader#readHttpRequest*/MTIzNDU2NzgxMjM0NTY3ODEyMzQ1Njc4/AES(driverLisenceId)/*/MASK$2(name)//SKIP(birthday)
app-node:
http-header-id-type: 1 # write log/trace id to the http response header; 0 default no write header, 1 trace id, 2 step log id
application-name: ${spring.application.name}
host-id: node-123.test.test # unique, with file to identify unique file
2. Initiate Logger
// In Spring
public static void main(String[] args) {
io.bitryon.logger.boostrap.LoggingProxyInitiation.premain(null); // must load before everything.
//or add https://github.com/FrankNPC/bitryon-logging-tracing-examples/blob/master/bitryon-logging-java-spring-example/src/main/resources/META-INF/spring.factories
new SpringApplicationBuilder(ServerBootApplication.class).run(args);
}
// In java
public static void main(String[] args) {
LoggingProxyInitiation.premain(null);
LoggerProvider provider = LoggerProvider.getProvider("bitryon_log.properties", null);
new LoggingMethodIntercepter(provider);
}
3. Load Logger
// write step log id (trace id) header to the HTTP client, in your bean
public class ExampleWebHTTPClient {
@Resource
LoggerProvider bitryonLoggerProvider;
public RestClient getRestClient() {
return RestClient
.builder(new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpClient())))
.baseUrl(getBaseUrl())
// write step log id to the http request header to the next app/service to form traces
.requestInterceptor(new LoggingHttpClientHeaderWriterInterceptor(bitryonLoggerProvider))
.build();
}
}
// read step log id (trace id) header by filter in spring web
@Configuration
@Import(value= {
AutoConfigurationBitryonLogger.class, // to load the configs from application.yml for logging related beans
})
public class ExampleWebServerConfiguration {
@Resource
LoggerProvider bitryonLoggerProvider;
// read step log id from last app/service/http request header to form traces, and log http payload for specific paths
@Bean
FilterRegistrationBean<LoggingHttpRequestWebFilter> loggingResetInFilter() {
FilterRegistrationBean<LoggingHttpRequestWebFilter> reg = new FilterRegistrationBean<>(
new LoggingHttpRequestWebFilter(bitryonLoggerProvider, true));
reg.setOrder(Ordered.HIGHEST_PRECEDENCE);
reg.addUrlPatterns("/api", "/api/*", "/remote_api/*");
return reg;
}
}
4. Write logs
import io.bitryon.logger.annotation.Logging;
import io.bitryon.logger.Logger;
import io.bitryon.logger.provider.LoggerFactory;
@Service
public class MedicService {
private static final Logger logger = LoggerFactory.getLogger();
@Logging // just need to annotate class or method
public List<MedicConditionDO> query(String sessionId, Page page){
logger.text("write plain text");
logger.log("write json", page);
return null;
}
}
NoteWe only completed logging and tracing.
Next steps
Sign-in portal for workflow views.
Explore examples on GitHub.