log print log
Printing logs play a key role in software development, system management and security management, providing necessary information and data for problem troubleshooting, performance optimization, security auditing, monitoring and historical recording, especially in large-scale and complex applications and systems. Printing logs is very necessary.
Luwak also provides rich log printing functions, following the usage of log4j.
Log level
Luwak provides four log levels in log4j to control the verbosity and output of logging. The following are the log levels from low to high:
| Level | Method | Purpose | | ------------- | ----------- | ----------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ----- | | DEBUG (debugging) | log.debug() | Used to record debugging information to help developers diagnose problems and track the code execution process. Use this level for detailed logging during development and testing phases. For example, luwak automatically derives SQL (prepared statement), as well as placeholder binding values, etc. | | INFO (information) | log.info() | Used to record normal running information of the program, such as application startup, completion of key operations, etc. This is the log level enabled by default in production environments. | | WARN (warning) | log.warn() | Used to record warning information that may cause potential problems, but does not affect the continued operation of the program. For example, outdated APIs are used, configuration items are missing, etc. | | ERROR (error) | log.error() | Used to record error information, indicating that the program has encountered a recoverable exception or error condition. These errors do not cause the program to terminate, but require attention and handling. |
By setting the log level in luwak's configuration file, you can control which levels of logs will be recorded and output. The log level can be reasonably selected according to specific needs and scenarios to balance the log volume and the readability of the information. In a production environment, the log level is usually set to INFO or above to reduce unnecessary log output.
Log level configuration
In luwak's configuration file luwak.yaml, level is used to set the log level. Currently, debug and info levels are supported.
The configuration is as follows:
log:
level:debug
file_path: log/luwak.log
max_size: 500 # megabytes It defaults to 100 megabytes
max_backups: 3 # The default is to retain all old log files
max_age: 28 # days, The default is not to remove old log files
compress: true # disabled by default
Logging format
// log.info(), log.warn(), log.error(), or log.debug()
log.info("--hello-world--", result, {"a":1,"b":2}, [1,2]);
The relationship between log level configuration and printing logs
The log level set by level will print all logs greater than or equal to this level. For example, if set to DEBUG, logs of DEBUG and above levels will be printed, including DEBUG, INFO, WARN, and ERROR. Similarly, if set to ERROR, only ERROR logs will be printed.
Level | Print log type |
---|---|
DEBUG | DEBUG, INFO, WARN, ERROR |
INFO | INFO, WARN, ERROR |
WARN | WARN, ERROR |
ERROR | ERROR |
Example
The following takes the most commonly used DEBUG and INFO levels as examples to introduce their actual effects in luwak. The other levels will not be introduced one by one. If you are interested, you can try it yourself.
DEBUG level
level is configured as debug
log:
level:debug
file_path: log/luwak.log
max_size: 500 # megabytes It defaults to 100 megabytes
max_backups: 3 # The default is to retain all old log files
max_age: 28 # days, The default is not to remove old log files
compress: true # disabled by default
JavaScript code
//The following logs are set up to facilitate viewing the relationship between log levels and printing logs.
log.debug("this is a debug log");
log.info("this is an info log");
log.warn("this is a warning log");
log.error("this is an error log");
Printing effect
Open log/luwak.log and see the following printed content:
2023/10/26 13:53:22 DEBU js_log.go:62 this is a debug log
2023/10/26 13:53:22 INFO js_log.go:53 this is an info log
2023/10/26 13:53:22 WARN js_log.go:44 this is a warning log
2023/10/26 13:53:22 ERRO js_log.go:35 this is an error log
2023/10/26 13:53:22 DEBU dao_util.go:50 SELECT c.emp_no, c.first_name, c.last_name, c.gender FROM hrm.employees c WHERE c.emp_no = ? LIMIT 1 - [10003] - 5.253776ms
2023/10/26 13:53:22 DEBU server.go:241 127.0.0.1 | HTTP/1.1 | POST /console/js_code - 200 - 7.035471ms | Apache-HttpClient/4.5.13 (Java/11.0.11)
It can be seen that when the log level is DEBUG, logs of DEBUG and above levels are printed, including DEBUG, INFO, WARN, and ERROR.
INFO level
Adjust the log level in luwak's configuration file luwak.yaml to info.
After modifying the log level, you need to restart the luwak service, otherwise it will not take effect.
log:
level: info
file_path: log/luwak.log
max_size: 500 # megabytes It defaults to 100 megabytes
max_backups: 3 # The default is to retain all old log files
max_age: 28 # days, The default is not to remove old log files
compress: true # disabled by default
JavaScript code
//The following logs are set up to facilitate viewing the relationship between log levels and printing logs.
log.debug("this is a debug log");
log.info("this is an info log");
log.warn("this is a warning log");
log.error("this is an error log");
Printing effect
View the logs printed in log/luwak.log
2023/10/26 14:13:29 INFO js_log.go:53 this is an info log
2023/10/26 14:13:29 WARN js_log.go:44 this is a warning log
2023/10/26 14:13:29 ERRO js_log.go:35 this is an error log
It can be seen that when the INFO level log is turned on, only INFO and above logs are printed in the luwak log, and DEBUG below INFO will not be printed in the luwak log.
Other log levels will not be explained one by one here.