0

I have an issue when try to logs multiple time logs in the application

I have two file -log4j2.xml is default log, log anything hit my application -log4j2-by-user.xml it almost the same configure configure just difference log path and filename.

package com.abc.efg.utils;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
import org.springframework.util.StringUtils;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Objects;

public class LogUtil {

    private LogUtil() {
    }

    public static void write(String userId) {
        if (StringUtils.hasLength(userId)) {
            try {
                System.setProperty("LOG_BY_USER_ID", userId);
                String configFile = "log4j2-by-user.xml";
                LoggerContext context = (LoggerContext) LogManager.getContext(false);
                URI configUri = Objects.requireNonNull(LogUtil.class.getClassLoader().getResource(configFile)).toURI();
                context.setConfigLocation(configUri);
                context.updateLoggers();
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        }
    }
}

I want to start log where function called LogUtil.write and use the userId as fileName currently it work but it effect to log4j2.xml when that function is called. if request write to userid.log then app-log.log is not write.

LOG BY-USER
<Configuration status="WARN" monitorInterval="30">
    <Properties>
        <Property name="LOG_DIR_USER">C:/dev/logs/user</Property>
    </Properties>
    <Appenders>
        <RollingFile name="UserFile" fileName="${LOG_DIR_USER}/%X{userId}.log" filePattern="${LOG_DIR_USER}/%X{userId}-%d{yyyy-MM-dd}.log.gz">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy />
            </Policies>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="UserFile"/>
        </Root>
    </Loggers>
</Configuration>

//LOG BY DEFAUL
<Configuration status="WARN" monitorInterval="30">
    <Properties>
        <Property name="LOG_DIR">C:/dev/logs/</Property>
        <Property name="LOG_FILE">app-log</Property>
    </Properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
        <RollingFile name="File" fileName="${LOG_DIR}/${LOG_FILE}.log" filePattern="${LOG_DIR}/${LOG_FILE}-%d{yyyy-MM-dd}.log.gz">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy />
            </Policies>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="File"/>
        </Root>
        <Logger name="com.abc.abcef" level="info" additivity="false">
            <AppenderRef ref="File"/>
            <AppenderRef ref="Console"/>
        </Logger>
    </Loggers>
</Configuration>

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.