5.6.1. Setting up Logging in Tomcat

Running Gradle setupTomcat task installs the Tomcat server into the project directory and performs its additional configuration. Particularly, setenv.bat and setenv.sh files are created in the tomcat/bin subfolder, and log4j.xml is created in the tomcat/conf subfolder.

Among other things, the setenv.* files define loading parameters for the log4j.xml configuration file using the CATALINA_OPTS variable.

log4j.xml defines logging configuration. The file has the following structure:

  • appender elements define the "output device" for the log. The main appenders are FILE and CONSOLE. The Threshold parameter of the appender defines the message threshold. By default, it is DEBUG for a file and INFO for console. It means that ERROR, WARN, INFO and DEBUG messages are written to a file, while ERROR, WARN and INFO are written to console.

    The path to the log file for the file appender is defined in the File parameter; Append flag defines if the file should be flushed or appended to at server restart. The default settings are tomcat/logs/app.log and flush at restart. When an application is in production, we recommend setting Append to true.

    By default, the file appender is implemented by the org.apache.log4j.DailyRollingFileAppender class, which renames the log file to a previous date and starts a new file daily at 00:00:00. This helps avoiding excessively large log files.

  • category elements define the logger parameters that are used to send messages from the program code. Category names are hierarchical, i.e. the settings of the com.company.sample category have effect on the com.company.sample.core.CustomerServiceBean and com.company.sample.web.CustomerBrowse loggers, if the loggers do not explicitly override the settings with their own.

    Minimum logging level is defined by the priority element. For example, if the category is INFO, then DEBUG and TRACE messages will not be logged. It should be kept in mind that message logging is also affected by the level threshold set in the appender.

You can quickly change category levels and appender thresholds for a running server using the Administration > Server log screen available in the web client. Any changes to the logging settings are effective only during server runtime and are not saved to a file. The screen also allows viewing and loading log files from the server logs folder (tomcat/logs).

The platform automatically adds the following information to the messages written to a log:

  • [application] – the name of the Tomcat-deployed web application whose code has logged the message. This information allows identifying messages from different application blocks (Middleware, Web Client), since they are written into the same file.

  • [user] – login name of the user who invoked the code logging the message. This helps to track activity of a certain user in the common log. If the code that logged a message was not invoked within a specific user session, the user information is not added.

For example, the following message has been written to the log by the code of the Middleware block (app-core), running under the admin user:

2013-12-19 18:48:17,282 DEBUG [com.haulmont.cuba.core.app.DataManagerBean] [app-core] [admin] loadList: metaClass=sec$User, view=com.haulmont.cuba.security.entity.User/user.browse, query=select u from sec$User u, max=100