Hi, I want to have different log files written in dynamic folders and dynamic files names based on threadContext parameters.
This is my setup 1. I have 2 context variables, that I write to the ThreadContext, Session and Revision. 2. I configured my log4j2 to write to session-${ctx:sessionId}/rev-${ctx:revisionName}.log e.g. <Routing name="FILE_ROUTING_PER_REVISION"> <Routes pattern="$${ctx:revisionName}"> <!-- This route is chosen if ThreadContext has no value for key revisionName. --> <Route key="$${ctx:revisionName}"> <RollingFile name="Rolling-default" fileName="${env:APPLICATION_HOME}/log/default.log" filePattern="${env:APPLICATION_HOME}/log/${date:yyyy-MM}/default-%d{yyyy-MM-dd}-%i.log.gz"> <PatternLayout pattern="${pattern}" /> <Policies> <OnStartupTriggeringPolicy /> </Policies> </RollingFile> </Route> <!-- This route is chosen if ThreadContext has a value for revisionName The value dynamically determines the name of the log file. --> <Route> <RollingFile name="Rolling-${ctx:revisionName}" fileName="${env:APPLICATION_HOME}/log/session-${ctx:sessionId}/rev-${ctx:revisionName}.log" filePattern="${env:APPLICATION_HOME}/log/${date:yyyy-MM}/s-${ctx:sessionId}-rev-${ctx:revisionName}-%i.log.gz"> <PatternLayout pattern="${pattern_with_session_info}" /> <Policies> <OnStartupTriggeringPolicy /> </Policies> </RollingFile> </Route> </Routes> </Routing> 3. The log file full path name includes the information as well within the log entry itself in each line, since I defined it as part of the pattern. <Property name="pattern_with_session_info">%d{yyyy-MM-dd HH:mm:ss}, [session=%mdc{sessionId} rev=%mdc{revisionName}], %-5level, %c{1}, %msg%n%throwable</Property> When I run this configuration I encounter 2 issues 1) The first session indeed creates a folder e.g. session-5c03c and a file called rev-2c44b.log BUT it includes information from other sessions as well. E.g. session 5c041 below. 1/16/2017 17:19 [session=5c03c rev=2c44b] DEBUG FetchCoverageFilesJob Starting COV files Fetch for rev 2c44b451b5a39a7b3a79640bc39b4bb087c86b1f 1/16/2017 17:19 [session=5c03c rev=2c44b] DEBUG FetchCoverageFilesJob Fetching cov files: test 1/16/2017 17:19 [session=5c03c rev=2c44b] DEBUG FetchCoverageFilesJob Saving cov file 'test' to DB COMPLETED 1/16/2017 17:19 [session=5c03c rev=2c44b] DEBUG FetchCoverageFilesJob Saving cov file 'test' to DB COMPLETED for term * 1/16/2017 17:19 [session=5c041 rev=2c44b] DEBUG FetchCoverageFilesJob Starting COV files Fetch for rev 2c44b451b5a39a7b3a79640bc39b4bb087c86b1f 1/16/2017 17:19 [session=5c041 rev=2c44b] DEBUG FetchCoverageFilesJob Fetching cov files: test 2) After the first session folder was created all the other sessions does not create new folder and their entries goes to the first folder. My questions: 1) It is a configuration issue or a bug in the log4j? 2) Why the same parameter e.g. sessionId has 2 different names in the log4j.xml per it's location? In the routing definition I had to use -${ctx:sessionId} and in the entry itself I had to use %mdc{sessionId}. (I played a lot with this configuration till it started working, and this exact combination is the only one that seems to work) Is there a way to define it with the same name for both needs in the xml file. E.g ${ctx:sessionId} and %{ctx:sessionId}? 3) Is there other way to achieve my target - e.g. folder-session-xxx/revision-yyy.log while there are some sessions running on different threads and each one might set the thread context information at 'the same time'. (currently there is no synchronization mechanism between the threads). Thanks, Ido ________________________________ PLEASE NOTE: The information contained in this electronic mail message is intended only for the use of the designated recipient(s) named above. If the reader of this message is not the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution, or copying of this message is strictly prohibited. If you have received this communication in error, please notify the sender by telephone or e-mail (as shown above) immediately and destroy any and all copies of this message in your possession (whether hard copies or electronically stored copies).