Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7540901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:45:18+00:00 2026-05-30T07:45:18+00:00

I have a problem regarding to logback project. My requirement is I have to

  • 0

I have a problem regarding to logback project. My requirement is I have to create log properties dynamically. Let me explain this by an example.

My project creates socket communication with external system and it may have multiple sockets. For each socket, I want to have different log files which will contain the messages that is read and sent. To accomplish this, I create the logger for sockets programmatically. Problem is when I want to reconfigure loggers based on logback.xml (by adding scan=”true” or by reinitializing the logback), the loggers I created becomes unusable. How can I fixed that or can you advise me another solution?

This is my configuration file (logback.xml)

<?xml version="1.0" ?>
<configuration>
    <property name="HOME_PATH" value="/data/logs/myapp/" scope="CONTEXT" />
    <property name="MYAPP_LOG_FILE" value="myapp.log" />
    <property name="MYAPP_ROLLING_TEMPLATE" value="%d{yy-MM-dd}" scope="CONTEXT" />
    <property name="MYAPP_OLD_LOG_FILE" value="${MYAPP_LOG_FILE}.%d{yy-MM-dd}" />
    <property name="DEFAULT_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%file:%line] [%level] %msg%n" scope="CONTEXT" />

    <appender name="myAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${HOME_PATH}${MYAPP_LOG_FILE}</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${HOME_PATH}${MYAPP_LOG_FILE}.${MYAPP_ROLLING_TEMPLATE}</fileNamePattern>
        </rollingPolicy>

        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${DEFAULT_PATTERN}</pattern>
        </encoder>
    </appender>

    <logger name="com.myapp" level="DEBUG" additivity="false">
        <appender-ref ref="myAppender" />
    </logger>

    <root level="OFF">
    </root>
</configuration>

and here you can see how I create loggers programmatically (again, I do this only for socket logs).

public static Logger createLogger(String name) {
        ch.qos.logback.classic.Logger templateLogger = (ch.qos.logback.classic.Logger) LogUtil.getLogger("com.myapp");
        LoggerContext context = templateLogger.getLoggerContext();

        String logDir = context.getProperty("HOME_PATH");

        PatternLayoutEncoder encoder = new PatternLayoutEncoder();
        encoder.setPattern(context.getProperty("DEFAULT_PATTERN"));
        encoder.setContext(context);

        DefaultTimeBasedFileNamingAndTriggeringPolicy<ILoggingEvent> timeBasedTriggeringPolicy = new DefaultTimeBasedFileNamingAndTriggeringPolicy<ILoggingEvent>();
        timeBasedTriggeringPolicy.setContext(context);

        TimeBasedRollingPolicy<ILoggingEvent> timeBasedRollingPolicy = new TimeBasedRollingPolicy<ILoggingEvent>();
        timeBasedRollingPolicy.setContext(context);
        timeBasedRollingPolicy.setFileNamePattern(logDir + name + ".log." + context.getProperty("MYAPP_ROLLING_TEMPLATE"));
        timeBasedRollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(timeBasedTriggeringPolicy);
        timeBasedTriggeringPolicy.setTimeBasedRollingPolicy(timeBasedRollingPolicy);

        RollingFileAppender<ILoggingEvent> rollingFileAppender = new RollingFileAppender<ILoggingEvent>();
        rollingFileAppender.setAppend(true);
        rollingFileAppender.setContext(context);
        rollingFileAppender.setEncoder(encoder);
        rollingFileAppender.setFile(logDir + name + ".log");
        rollingFileAppender.setName(name + "Appender");
        rollingFileAppender.setPrudent(false);
        rollingFileAppender.setRollingPolicy(timeBasedRollingPolicy);
        rollingFileAppender.setTriggeringPolicy(timeBasedTriggeringPolicy);

        timeBasedRollingPolicy.setParent(rollingFileAppender);

        encoder.start();
        timeBasedRollingPolicy.start();

        rollingFileAppender.stop();
        rollingFileAppender.start();

        ch.qos.logback.classic.Logger logbackLogger = (ch.qos.logback.classic.Logger) LogUtil.getLogger(name);
        logbackLogger.setLevel(templateLogger.getLevel());
        logbackLogger.setAdditive(false);
        logbackLogger.addAppender(rollingFileAppender);

        return logbackLogger;
}

And this is how I reinitialize logback

private static void initializeLogback() {
    File logbackFile = new File(logFilePath);
    System.setProperty("logback.configurationFile", logbackFile.getAbsolutePath());
    StaticLoggerBinder loggerBinder = StaticLoggerBinder.getSingleton();
    LoggerContext loggerContext = (LoggerContext) loggerBinder.getLoggerFactory();

    loggerContext.reset();
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(loggerContext);
    try {
        configurator.doConfigure(logbackFile);
    } catch( JoranException e ) {
        throw new ColumbusRuntimeException(e.getMessage(), e);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T07:45:19+00:00Added an answer on May 30, 2026 at 7:45 am

    Looks like you need the SiftingAppender where your discriminator would be the socket id itself, or any combined variation. I dunno what threading issues you’ll run into with this (when MDC is read, etc), but this should be a good starting point and seems similar to your case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem regarding the use of hibernate.properties and c3p0.properties. I am dependent
I have this logical problem regarding on how to pass value from one JFrame
I have another problem regarding Git. This time I thoroughly searched Google and Stack
I have this problem regarding sql which i will be using in a webservice
I have some problem regarding converting/truncating a number. This is the condition: x.35 where
its my first question here on this site. well i have a problem regarding
i have a problem regarding the responseXML of ajax.. I have this code from
I have a problem regarding GridSplitter visiblity. In this, whatever I am hosting a
I have a problem regarding this two values from my textboxes (text, text2), seems
I have a problem regarding getting the path of a user control. The scenario

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.