0

my problem is : My application maintains three buildings, and each building has a different process.

So, using logback, I want to create a log which has a specification : each building will have a specific folder, and inside that specific folder of each building, there will be many log files, with each log file indicates a process.

My logback.xml right now is :

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
        </pattern>
    </encoder>
</appender>

<appender name="logAppender" class="ch.qos.logback.classic.sift.SiftingAppender">
    <discriminator>
        <key>processName</key>
        <defaultValue>unknown</defaultValue>
    </discriminator>
    <sift>
        <appender name="FILE-${processName}"
            class="ch.qos.logback.core.rolling.RollingFileAppender">
            <file>logs/${distributor}/${processName}.log</file>
            <layout class="ch.qos.logback.classic.PatternLayout">
                <pattern>%d [%thread] %level %mdc %logger{35} - %msg%n</pattern>
            </layout>
            <rollingPolicy
                class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
                <fileNamePattern>logs/${distributor}/${processName}.%i.log</fileNamePattern>
                <minIndex>1</minIndex>
                <maxIndex>10</maxIndex>
                <!-- <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> 
                    <maxFileSize>5KB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> -->
            </rollingPolicy>
            <triggeringPolicy
                class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
                <maxFileSize>10MB</maxFileSize>
            </triggeringPolicy>
        </appender>
    </sift>
</appender>

<logger name="processLog" level="debug" additivity="false">
    <appender-ref ref="logAppender" />
    <appender-ref ref="stdout" />
</logger>

<root level="debug">
    <appender-ref ref="stdout" />
    <appender-ref ref="logAppender" />
</root>
</configuration>

And my java servlet code is :

public class DistributorServlet extends HttpServlet {
   private static final long serialVersionUID = 1L;

   private static Logger processLog = LoggerFactory.getLogger("processLog");

   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      String office = req.getParameter("office");
      MDC.put("distributor", office);
      String process = req.getParameter("process");
      MDC.put("process", process);
      processLog.debug("Processing");
   }
}

However, a log file is not generated.

Can anyone help me?

Thank you very much

xemjas
  • 115
  • 4
  • 16

1 Answers1

0

1. Make the below change

private static Logger processLog = LoggerFactory.getLogger("processLog");

to

private static Logger processLog = LoggerFactory.getLogger(DistributorServlet .class);

2. Add additional discriminator for distributor

From the logback.xml it appears that only one discriminator has been added. Did you try adding another one for distributor

3. Do not forget

To add MDC.remove("keyName"); after its usage.

4. In case if you observe issue with multiple MDC keys

I faced an issue with the MDC.put in trying to add multiple keys into it. (I wondered why no putAll has been defined)

Although the underlying implementation is that of a HashMap<Key k, Value v> that should allow adding multiple keys, I was only able to see that the last key you put into MDC would be applied to logback.xml

While for the other keys I observed _IS_UNDEFINED value that gets appended instead.

Of course then again you can refer to the other various links and although this may not be a best idea, here is what I have done in my Java code,

System.setProperty("distributor", req.getParameter("office"));

System.setProperty("process", req.getParameter("process"));

Modify the logback.xml by removing discriminator. Well, alternatively you can remove one of the above properties and have the MDC.put for that property.

However please do refer to the links System.setProperty is safe in java?

Alternatively I suggest https://stackoverflow.com/a/32615172/3838328

Community
  • 1
  • 1
Kamal Kunjapur
  • 8,547
  • 2
  • 22
  • 32