The tomcat console under windows outputs catalina.out logs on a daily basis

In the case of windows server, you cannot follow the same process as Linux server. After starting the web service, you can directly tail to view the log. However, the output space of the window console is limited. If a large number of errors are encountered, the console log refreshes too quickly and the first error cannot be seen. Where it appears will cause a lot of unnecessary trouble, so I looked up the method of outputting Windows console information to a file on the Internet and recorded it.

catalina.date.log
1. Mainly records information when tomcat is started;
2. Log information such as startup JVM parameters and operating system;
3. Log mechanisms such as log4j are not used, and system error messages and print statements are also in this log Recorded in the file;
4. In the Linux environment, all logs are in catalina.out.
5. In the windows environment, the content in catalina.out is empty. The log will only be printed on the console

Method 1 (the console window has only a small amount of information, and the running log is stored in catalina-log)

Step 1: Open the startup.bat file under bin,

把 call "%EXECUTABLE%" start %CMD_LINE_ARGS%
改为 call "%EXECUTABLE%" run %CMD_LINE_ARGS%
此修改:不会弹出新的cmd窗口,直接在本窗口运行

Step 2: Open the catalina.bat file under bin (at the end of the file) and
add after each %ACTION%

>> %CATALINA_HOME%\logs\catalina.%date:~0,4%-%date:~5,2%-%date:~8,2%.out

The generated log is as follows.
Insert image description here
Double-click the startup.bat file to start the tomcat service.

Result:
The console will not print the running log (only a small amount of information), the running log (including some error messages) will be stored in the log file in the ./log directory, and can only be viewed in the file. It is not conducive for maintenance personnel to directly judge whether the project is successfully launched.

Method 2 (there is no content in the console window, the running log is stored in catalina-log)

Steps: Open the startup.bat file under bin,

把 call "%EXECUTABLE%" start %CMD_LINE_ARGS%
直接改为 call "%EXECUTABLE%" run %CMD_LINE_ARGS% >>%CATALINA_HOME%\logs\catalina.%date:~0,4%-%date:~5,2%-%date:~8,2%.out 2>&1

Changing start to run is to modify the startup mode of tomcat. What is added after >> is to generate the catalina.year-month-day.out log storage file in the logs folder in the tomcat installation directory.

After the modification is completed, double-click startup.bat to start the tomcat server and you will find that there is no content in the command window. This is correct.
Insert image description here
The generated log is as follows
Insert image description here

Due to the configuration of the run startup mode, the window name configuration of the catalina.bat file has become invalid.
You can only reconfigure it in the startup.bat file.
Insert image description here

Result:
There is nothing in the command window. It is also not conducive for maintenance personnel to directly judge whether the project is started successfully. They can only check the log file to know whether the project is started successfully.

Method three (can print to the console and store logs)

Through the above two methods, we know that after the startup.bat mode under Windows is changed to run,
the console has no way to output the content. You can only judge the startup status of the project in the generated log file,
or the console prints and there is no log. Storage;
or log storage; no console printing;
both cannot be available at the same time, which makes people speechless.

Is there any way to achieve this? Finally I found it, I need to use tee.exe to achieve this effect.

Download UnxUtils.zip at http://sourceforge.net/projects/unxutils/?source=dlp,
Please add image description
unzip and copy tee.exe under UnxUtils\usr\local\wbin to windows\system32.

Open the startup.bat file under bin,
add chcp 65001 at the end and modify call “%EXECUTABLE%” start %CMD_LINE_ARGS%
as follows:

chcp 65001

call "%EXECUTABLE%" run %CMD_LINE_ARGS% 2>&1 | tee %CATALINA_HOME%\logs\catalina.%Date:~0,0%%Date:~3,4%-%Date:~8,2%-%Date:~11,2%.out

If the execution is unsuccessful, it may be that the chcp command cannot be executed under cmd. If
this error occurs (chcp is not an internal or external command, nor an operable program), you need to configure the environment variables.

Add the path C:\WINDOWS\system32 under the system variable PATH (it is best to copy the path on the computer, copy it directly here, there may be symbol escape)
My environment is a screenshot under Windows 10
Insert image description here
, if it is Windows xp or Windows 7 , remember to add ";" in front of it (if you already have it, don't add it).
The complete version is
;C:\WINDOWS\system32

The configuration here may need to restart the computer to take effect (I did not restart it myself, maybe I have configured this variable before)

Finally, I achieved both log storage and console printing.

Related links
https://blog.csdn.net/weixin_42250959/article/details/103474660
https://www.cnblogs.com/dannylinux/p/10873993.html
https://www.jianshu.com/p/22202f4268afTitle
The title is garbled after changing to Chinese:
https://blog.csdn.net/lixingecho/article/details/115945423
Save the tomcat log under Windows and at the same time the console outputs
https://blog.csdn.net/itlijinpeng/article/details /129142114

Guess you like

Origin blog.csdn.net/qq_20236937/article/details/134218355