Log¶
Topics
Summary¶
Log is generated for the purpose of internal control, security attainment, and maintenance.This section describes the log implementation in TERASOLUNA Global Framework on Accel Platform.
Utilizing Logger API¶
Usage of Logger(jp.co.intra_mart.common.platform.log.Logger) is described.
Log Level¶
In the Logger API 5 log levels are provided.
trace(most trivial) debug info warn error(most serious)
Getting Logger object¶
public int add(final int value1, final int value2) { // Get logger instance // Since logger name is not specified, class name becomes the logger name // Class Name:tutorial.controller.AddController // Logger Name:tutorial.controller.AddController final Logger logger = Logger.getLogger(); return 0; }Logger object is obtained by using Logger#getLogger() method. Character string passed to the argument will become the logger name.If nothing is passed to the argument, class name called (FQCN) will become the logger name.
Log Output¶
public int add(final int value1, final int value2) { // Get logger instance // Since logger name is not specified, class name becomes the logger name // Class Name:tutorial.controller.AddController // Logger Name:tutorial.controller.AddController final Logger logger = Logger.getLogger(); logger.debug("arguments=[{}, {}]", value1, value2); final int result = value1 + value2; logger.trace("result={}", result); return result; }Log output is generated using the Logger object.In the function above, the value passed to the argument is generated at the debug level and the computed result is generated at the trace level.If add (1, 2) is executed at the log level debug, output will be as follows.[DEBUG] t.c.AddController - arguments=[1, 2]If add (1, 2) is executed at the log level trace, output will be as follows.[DEBUG] t.c.AddController - arguments=[1, 2] [TRACE] t.c.AddController - result=3
Utilizing MDC API¶
Usage of MDC(jp.co.intra_mart.common.platform.log.MDC) is described.
MDC¶
By using the Mapped Diagnostic Context, it is possible to output the information, which is saved by the key uniquely defined by the layout setting of log setting file, to the log.By using the MDC API, it is possible to write information to the key which is uniquely defined.
Output Log which uses MDC¶
// Define MDC Key private static final String MKC_FUNC_KEY = "application.func"; public int add(final int value1, final int value2) { // Get logger instance // Since the logger name is not specified, class name becomes the logger name. // Class Name:tutorial.controller.AddController // Logger Name:tutorial.controller.AddController final Logger logger = Logger.getLogger(); // Set value to MDC MDC.put(MKC_FUNC_KEY, "add"); logger.debug("arguments=[{}, {}]", value1, value2); final int result = value1 + value2; logger.trace("result={}", result); // Initialize MDC value MDC.remove(MKC_FUNC_KEY); return result; }Name of the function which is being executed is set to MDC.In the MDC#put(key, value) method, name of the function “add” is set to the MDC key “application.func”.Contents that are saved in MDC will not be initialized as long as it is not explicitly initialized.Therefore, after the completion of target log output process, the value of MDC key “application.func” is initialized by the MDC#remove(key) method.Output ExampleChange the contents of <configuration>/<appender name=”STDOUT”>/<encoder>/<pattern> of <CONTEXT_PATH>/WEB-INF/conf/log/im_logger.xml as follows, and restart the application server.[%level] %logger{10} - %X{application.func} %msg%nLog Output when add(1, 2) is executed at log level trace[DEBUG] t.c.AddController - add arguments=[1, 2] [TRACE] t.c.AddController - add result=3
About TERASOLUNA Global Framework on Accel Platform Log Output Setting¶
Setting of log output is stated in WEB-INF/conf/log/im_logger_tgfw.xml.Log is output to the WEB-INF/platform/im_tgfw.log file as standard.Please change the setting according to the operation environment.Standard setting is as follows.
Logger Name Log Level jp.co.intra_mart.framework.extension.spring warn jp.co.intra_mart.system.router.spring warn org.springframework warn org.springframework.web.servlet info org.terasoluna.gfw info org.terasoluna.gfw.common.exception.ExceptionLogger info org.terasoluna.gfw.common.exception.ExceptionLogger.Monitoring none
Utilizing TraceLoggingInterceptor of TERASOLUNA Global Framework¶
TraceLoggingInterceptor is the function to output the process time of method which is mapped to the request URL.In TERASOLUNA Global Framework on Accel Platform this function can be used by setting applicationContext-im_tgfw_web.xml and im_logger_tgfw.xml.For the details of TraceLoggingInterceptor, please refer to TERASOLUNA Global Framework Development Guideline and see TraceLoggingInterceptor item.applicationContext-im_tgfw_web.xml SettingAdd mvc:interceptorsにTraceLoggingInterceptor setting.<!-- MVC interceptors --> <mvc:interceptors> <!-- omitted --> <!-- TraceLoggingInterceptor is added. --> <mvc:interceptor> <mvc:mapping path="/**" /> <bean class="org.terasoluna.gfw.web.logging.TraceLoggingInterceptor"> <property name="warnHandlingNanos" value="1000000000" /> </bean> </mvc:interceptor> </mvc:interceptors>Note
path attribute of mvc:mapping should be set by filtering to the package of the target Controller class for trace log output.im_logger_tgfw.xml SettingAdd log output setting of org.terasoluna.gfw.web.logging.TraceLoggingInterceptor.<logger name="org.terasoluna.gfw.web.logging.TraceLoggingInterceptor" additivity="false"> <level value="trace" /> <appender-ref ref="IM_TGFW" /> </logger>