logging - How to pass log file name through MDC logback feature -
I want to use the MDC feature logback for my log name, but I get the % PARSER_ERROR [X]
Here is my logback.xml file contents:.
& lt; XML version = "1.0" encoding = "UTF-8" & gt; & Lt; Configuration scan = "true" scan Pieriod = "60 seconds" & gt; & Lt; JmxConfigurator / & gt; & Lt; Property scope = "context" name = "PATH_TO_LOGS" value = "$ {catalina.base} / log" /> & Lt; Appender name = "FILE" class = "ch.qos.logback.core.rolling.RollingFileAppender" & gt; & Lt; File & gt; $ {PATH_TO_LOGS} /app.log</file> & Lt; Filter class = "ch.qos.logback.classic.filter.ThresholdFilter" & gt; & Lt; Levels & gt; Error & lt; / Level> & Lt; / Filter & gt; & Lt; Rolling policy class = "ch.qos.logback.core.rolling.FixedWindowRollingPolicy" & gt; & Lt; fileNamePattern & gt; $ {PATH_TO_LOGS} /% X {myDefinedName} .app% i.log & lt; / fileNamePattern & gt; & Lt; MaxIndex & gt; 10 & lt; / maxIndex & gt; & Lt; / RollingPolicy & gt; & Lt; Trigger Policy Category = "ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy" & gt; & Lt; MaxFileSize & gt; 10 MB & lt; / maxFileSize & gt; & Lt; / TriggeringPolicy & gt; & Lt; Encoder & gt; & Lt; Pattern & gt;% d {ISO8601}% -5level [% thread]% Logger {36} - [% X {myDefinedName}] -% Message% n & lt; / Pattern & gt; & Lt; / Encoder & gt; & Lt; / Appender & gt; & Lt; Root level = "warning" & gt; & Lt; Epander-Reef Ref = "FILE" /> & Lt; / Root & gt; & Lt; / Configuration & gt; Edit: and here is my filter
private static final String NAME_TO_PASS = "myDefinedName"; Private string getParameter (servlet request request) {HttpServletRequest req = (HttpServletRequest) request; String parameter = req.getParameter (NAME_TO_PASS); Http session session = req.getSession (); If (parameter! = Null) {session.setAttribute (NAME_TO_PASS, parameter); } And {parameter = (string) session.getAttribute (NAME_TO_PASS); } Return parameter; } @Override public void init (FilterConfig filterConfig) throws ServletException {} @Override public void doFilter (ServletRequest request, ServletResponse response, FilterChain series) throws IOException, ServletException {try {MDC.put (NAME_TO_PASS, getParameter (request)); Chain.doFilter (request, response); } Finally {MDC.remove (NAME_TO_PASS); }} @Override destroy public void () {} Somewhere I read that I must use $ {myDefinedName} % X { Pattern instead of myDefinedName} , but I can not see it correctly, because logs are created on the app deployment process without the MDC parameter. How to start logging only when the MCD parameter is passed?
used the official example SiftingAppender and it works:
< Code> & lt; Configuration & gt; & Lt; Appender name = "FILE" class = "ch.qos.logback.classic.sift.SiftingAppender" & gt; & Lt ;! - In the absence of class character, it is believed that the desired differential type ch.qos.logback.classic.sift.MDCBasedDiscriminator - & gt; & Lt; Discriminator & gt; & Lt; Key & gt; MyDefinedName & lt; / Key & gt; & Lt; DefaultValue & gt; Unknown & lt; / DefaultValue & gt; & Lt; / Discriminator & gt; & Lt; Filtering & gt; & Lt; Epander Name = "FILE - $ {myDefinedName}" class = "ch.qos.logback.core.FileAppender" & gt; & Lt; File & gt; $ {MyDefinedName} log & lt; / File & gt; & Lt; Attached & gt; False & lt; / Enclosed & gt; & Lt; Layout class = "ch.qos.logback.classic.PatternLayout" & gt; & Lt; pattern & gt;% d [% thread]% level% mdc% logger {35} -% msg% n & lt; / Pattern & gt; & Lt; / Layout & gt; & Lt; / Appender & gt; & Lt; / Filtering & gt; & Lt; / Appender & gt; & Lt; Root level = "warning" & gt; & Lt; Epander-Reef Ref = "FILE" /> & Lt; / Root & gt; & Lt; / Configuration & gt; If that MDC value is zero, then the default value is used as a differentiation value for the name log name.
Comments
Post a Comment