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 7089913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:58:49+00:00 2026-05-28T07:58:49+00:00

How can I change a log file path on the fly in Logback? In

  • 0

How can I change a log file path on the fly in Logback?

In my web application user can change the place where some e-mail attachments will be saved and processed. The user can access this path and see if happened some errors after processing, so I want to create a log file in this path. To accomplish this I’ve tried use this Logback configuration:

<configuration debug="true" scan="true" scanPeriod="30 seconds">
    <property scope="context" resource="com/hrgi/configuracoes/recuperadorNFe.properties" />
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%date{dd/MM/yyyy;HH:mm:ss} [%thread] %-5level %logger - %msg%n %ex{full}</pattern>
        </encoder>
    </appender>
    <appender name="importador" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${recuperadorNFe.diretorioDeArmazenamento}/logs/importador.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${recuperadorNFe.diretorioDeArmazenamento}/logs/%d{yyyy/MM}/importador-%d{dd}.log</fileNamePattern>
        </rollingPolicy>
        <encoder>
            <pattern>(%date{dd/MM/yyyy;HH:mm:ss} [%-5level]) -> %msg%n %nopex</pattern>
        </encoder>
    </appender>
    <logger name="com.hrgi.nfe" level="info">
        <appender-ref ref="importador" />
    </logger>
    <root level="info">
        <appender-ref ref="console" />
    </root>
</configuration>

Like you can see, my hope was restart logger context so perhaps it could see that the properties file’s content was changed and load the right values. Doesn’t work:

private void restartLogContext(){
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    loggerContext.stop();
    loggerContext.start();
    StatusPrinter.print(loggerContext);
}

So I’ve tried remove logger appender and create a new one. At least the log file was created in the new path, but the logging is happening in console:

private void restartLog(){
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();

    Logger logbackLogger = loggerContext.getLogger("com.hrgi.nfe");
    logbackLogger.detachAndStopAllAppenders();

    TimeBasedRollingPolicy policy= new TimeBasedRollingPolicy();
    policy.setContext(loggerContext);
    policy.setFileNamePattern(modeloPopup.getConfiguracoes().getDiretorioDeArmazenamento()+"/logs/%d{yyyy/MM}/importador-%d{dd}.log");

    RollingFileAppender fileAppender = new RollingFileAppender();
    fileAppender.setContext(loggerContext);
    fileAppender.setName("importador");
    fileAppender.setFile(modeloPopup.getConfiguracoes().getDiretorioDeArmazenamento()+"/logs/importador.log");
    fileAppender.setRollingPolicy(policy);

    policy.setParent(fileAppender);

    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setContext(loggerContext);
    encoder.setPattern("(%date{dd MMM yyyy;HH:mm:ss} [%-5level]) -> %msg%n %nopex");
    encoder.start();

    fileAppender.setEncoder(encoder);
    fileAppender.start();

    logbackLogger.addAppender(fileAppender);        
    StatusPrinter.print(loggerContext);
}

Could someone explain me what I’m doing wrong?

  • 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-28T07:58:50+00:00Added an answer on May 28, 2026 at 7:58 am

    I’d just extend ch.qos.logback.core.rolling.TimeBasedRollingPolicy and pass it to RollingFileAppender.

    It looks like appender can use it as a triggering and rolling policy. Just make it do rollover every time configuration changes somewhere and return next file name based on your configuration.
    You should look at the methods isTriggeringEvent – this makes appender rollover, and rollover – this actually does a rollover, and getNewActiveFileName.

    Look at (RollingFileAppender source)[http://grepcode.com/file/repo1.maven.org/maven2/ch.qos.logback/logback-core/0.9.3/ch/qos/logback/core/rolling/RollingFileAppender.java] to get the idea how to hook it up. It should be easier than struggling with configuration.

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

Sidebar

Related Questions

How can I change my working directory to the path of the file that
Can someone tell me how i can change the .xml file that a flash
I am making a small change to an existing application so that users can
You can change the connection string at run-time like this. You make the connection
I can change SQL at runtime. Can I do the same with LINQ ?
I can change the irb prompt mode with irb --prompt prompt-mode I can see
I understand how I can change the dns settings for my domains by editing
Where I can change compiler options for C# project in VisualStudio 2008 (without command
I know I can change the selectionStyle of a UITableViewCell to make it highlight
I understand that I can change a sql table using the follow sp: EXEC

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.