Whether we run the jar package under Windows or LInux, we will see logs on the console. It is definitely inconvenient to view it directly. Therefore, the contents of the console need to be saved to a file for management and viewing. It can be divided into two types: normal log and error log.
java -jar xxx.jar
This way of printing logs will always be printed on the console, which is inconvenient for management.
We output the console logs to the specified file.
java -jar xxx.jar > sysMsg.log 2>&1 &
Parameter explanation:
0 standard input (usually the keyboard)
1 standard output (usually the display screen, the user terminal console)
2 standard error (error message output)
The following demonstrates three commonly used cases. Before using, create standard log files and error log files.
# 标准日志文件
touch sysMsg.log
# 错误日志文件
touch err.log
1. Standard logs are output to the /xxx.log file, and error logs are input to the /exxx.log file.
java -jar xxx.jar > /xxx.log 2> /exxx.log &
The locations of the standard log files and error log files here need to be specified by absolute paths (the locations are customized according to the location of the standard log files and error files you created)
- View standard log files
vim sysMsg.log
- View error log file
The error here was made intentionally by me. You will only see the error message when you open the file.
vim err.log
2. The standard log is output to the /xxx.log file, and the error log is also input to the /xxx.log file.
java -jar xxx.jar > /xxx.log 2>&1 &
This means that no matter what log it is, it is placed in the same file.
- View the generated log file
You can see that both standard logs and errors exist
vim sysMsg.log
3. Standard logs are output to /dev/null (standard logs are not output), and error logs are input to the /exxx.log file.
java -jar xxx.jar > /dev/null 2> /exxx.log &
Normal log files do not need to be recorded, and error logs are recorded directly to the file.
The error here was made intentionally by me. You will only see the error message when you open the file.
- View error log
vim err.log
Use the command to view the log:
1. Print the last 300 lines of logs and continue to track the logs. ctrl+c exit
tailf -n 300 xxx.log
Print the last 300 lines of the error log
2. Check the end of the log. New logs will be scrolled and updated in real time. ctrl+c exit
tail -f xxx.log