Spring Boot Annotation for Operation Logging (BizLog SDK)
This article introduces a Spring Boot component that automatically records who performed which operation at what time, explains its Maven dependency, configuration, usage of @EnableLogRecord and @LogRecordAnnotation with SpEL expressions, custom operators, detail fields, log categories, and extensibility through custom parse functions and service implementations.
The component solves the problem of capturing "who" performed "what" at "when" by automatically generating operation logs for Spring Boot applications.
It provides an autoconfiguration for Spring Boot; for Spring MVC you can initialize the beans via XML.
Maven dependency:
<dependency>
<groupId>io.github.mouzt</groupId>
<artifactId>bizlog-sdk</artifactId>
<version>1.0.4</version>
</dependency>Enable the log record switch in the Spring Boot entry class with @EnableLogRecord(tenant = "com.mzt.test") .
Basic log annotation example:
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableTransactionManagement
@EnableLogRecord(tenant = "com.mzt.test")
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}Use @LogRecordAnnotation to define log templates. Example for a successful order creation:
@LogRecordAnnotation(success = "{{#order.purchaseName}}下了一个订单,购买商品「{{#order.productName}}」,下单结果:{{#_ret}}",
prefix = LogRecordType.ORDER, bizNo = "{{#order.orderNo}}")
public boolean createOrder(Order order) {
log.info("【创建订单】orderNo={}", order.getOrderNo());
// db insert order
return true;
}The log will output something like "张三下了一个订单, 购买商品「超值优惠红烧肉套餐」, 下单结果: true".
To record failure logs, add a fail attribute; if an exception is thrown, the fail template is used, otherwise the success template is recorded.
Log categories (e.g., MANAGER ) allow separating user and operator logs for the same business key.
The detail field can store additional information such as the full toString() of the business object.
Operator can be specified manually via the operator attribute or automatically by implementing IOperatorGetService and registering it as a Spring bean.
Example of a custom operator service:
@Configuration
public class LogRecordConfiguration {
@Bean
public IOperatorGetService operatorGetService() {
return () -> Optional.of(OrgUserUtils.getCurrentUser())
.map(a -> new OperatorDO(a.getMisId()))
.orElseThrow(() -> new IllegalArgumentException("user is null"));
}
}Persisting logs can be customized by implementing ILogRecordService . A simple DB implementation is shown:
@Service
public class DbLogRecordServiceImpl implements ILogRecordService {
@Resource
private LogRecordMapper logRecordMapper;
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void record(LogRecord logRecord) {
log.info("【logRecord】log={}", logRecord);
LogRecordPO logRecordPO = LogRecordPO.toPo(logRecord);
logRecordMapper.insert(logRecordPO);
}
// query methods omitted for brevity
}Custom parse functions extend the template language. Examples include OrderParseFunction , UserParseFunction , and DiffListParseFunction , each implementing IParseFunction to transform SpEL values into readable strings.
Notes: the log is recorded after method execution, so any modifications to method parameters are reflected in the SpEL expressions.
Source code repository: https://github.com/mouzt/mzt-biz-log
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.