Log¶
Items
Overview¶
Log is output with the intention of internal control, to ensure security and maintenance, etc.In this article log implementation carried out by SAStruts framework is mentioned.
To Use Logger API¶
It mentions about the usage techniques of Logger (jp.co.intra_mart.common.platform.log.Logger)
Log level¶
5 log levels are provided in Logger API.
trace (most insignificant) debug info warn error (most significant)
To Acquire Logger Object¶
public int add(final int value1, final int value2) { // Acquire logger instance // Class name becomes the logger name, as the logger name is not specified // Class name: tutorial.action.AddAction // Logger name: tutorial.aciton.AddAction final Logger logger = Logger.getLogger(); return 0; }Acquire Logger object by using Logger#getLogger()method. String passed on to argument becomes the logger name.If nothing is passed on to argument, the called class name (FQDN) becomes the logger name.
To Output the Log¶
public int add(final int value1, final int value2) { // Acquires logger instance // Class name becomes the logger name, as the logger name is not specified // Class name: tutorial.action.AddAction // Logger name: tutorial.aciton.AddAction final Logger logger = Logger.getLogger(); logger.debug("arguments=[{}, {}]", value1, value2); final int result = value1 + value2; logger.trace("result={}", result); return result; }Output the log by using Logger object.In the above mentioned function, the value passed on to argument is output at debug level and the calculation result is output at trace level.When add(1, 2) is executed at log level debug, the output is as follows.[DEBUG] t.a.AddAction - arguments=[1, 2]When add(1, 2) is executed at log level trace, the output is as follows.[DEBUG] t.a.AddAction - arguments=[1, 2] [TRACE] t.a.AddAction - result=3
To Use MDC API¶
It mentions about the usage technique of MDC(jp.co.intra_mart.common.platform.log.MDC).
MDC¶
The information stored in a key which is uniquely defined in layout settings of log configuration file can be output in log by using Mapped Diagnostic Context.The information of the key which is uniquely defined can be written by using MDC API.
To output log where MDC has been used.¶
// MDC key definition private static final String MKC_FUNC_KEY = "application.func"; public int add(final int value1, final int value2) { // acquire logger instance // class name becomes the logger name, as the logger name is not specified // class name: tutorial.action.AddAction // logger name: tutorial.aciton.AddAction 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; }The function identifier which is being executed is set to MDC.The “add” function identifier that is being executed is set in MDC key “application.func” by MDC#put(key, value) method.The value of the contents stored in MDC will not be initialized unless it is explicitly done.Therefore, after the completion of intended log output process, the value of “application.func” of MDC key is initialized by 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 below 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.a.AddAction - add arguments=[1, 2] [TRACE] t.a.AddAction - add result=3