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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:52:45+00:00 2026-05-14T02:52:45+00:00

I have a web page, used for admin purposes, which runs a task (image

  • 0

I have a web page, used for admin purposes, which runs a task (image fetching from a remote site).
In order to be able to debug the task using the browser only, no ssh etc, I’d like to be able to read all log output from the executing thread and spit it out to the web page.
The task boils down to:

  1. Changing log level for current thread at the beginning of the call and restore when the call is done.
  2. Reading all log output by current thread and storing it in a string.

So in pseudocode my execute() method would look like this: (I’m using struts2)

public String execute() throws Exception {
  turnLoggingLevelToDebugOnlyForThisThread()
  ... do stuff...
  restoreLoggingLevelForThisThread()
  String logs = readAllLogsByThisThread();
}

Can this be done with log4j?

I’m using tomcat, struts2, log4j and slf4j.

EDIT 1: I should note that the motivation is to be able to see the existing logs on a web page without needing to add new log lines in code. Think of a nice web debug interface that lets you run your operation and the result spits out the logs of the operation.
EDIT 2: I should also note that I’m already using log4j (via slf4j) and a log4j.xml, so the solution I’m seeking needs to live aside the currently logging system, not ruin it.

  • 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-14T02:52:45+00:00Added an answer on May 14, 2026 at 2:52 am

    You can create a log4j appender to write to a StringWriter. Here is an example I did some time ago:

    consoleWriter = new StringWriter();
    WriterAppender appender = new WriterAppender(new PatternLayout("%d{ISO8601} %p - %m%n"),consoleWriter);
    appender.setName("CONSOLE_APPENDER");
    appender.setThreshold(org.apache.log4j.Level.ERROR);
    Logger.getRootLogger().addAppender(appender);
    

    It writes all ERROR logs to the consoleWriter, but you can set the scope or the level of the logs as you wish. The scope (logger name) should be a unique identifier of the thread. something like this:

    Logger.getLogger("Thread-00001").addAppender(appender);
    

    Your thread should write to this logger.

    Logger.getLogger("Thread-00001").info("blah blah blah");
    

    And when you want to finish logging the thread:

    Logger.getLogger("Thread-00001").removeAppender("CONSOLE_APPENDER");
    

    UPDATE:
    Here is a working example. Writes error logs to a file (set in log4j.xml) + writes all thread logs to a StringWriter, when enabled:

    import java.io.StringWriter;
    import org.apache.log4j.Logger;
    import org.apache.log4j.Level;
    import org.apache.log4j.WriterAppender;
    import org.apache.log4j.PatternLayout;
    
    public class Log4jTest implements Runnable {
    
        public static final String CONSOLE_APPENDER = "CONSOLE_APPENDER";
        private static WriterAppender appender = null;
        private static int counter = 1;
    
        public static synchronized String getNextId() {
            return "Thread_00"+(counter++);
        }
    
        public void run() {
            String id="UNKNOWN";
            try {
                id = getNextId();
                Logger log = Logger.getLogger(id);
                log.addAppender(appender);
                log.setLevel(Level.DEBUG);
                log.info(id+" log message 1");
                log.removeAppender(CONSOLE_APPENDER);
                log.info(id+" log message 2");
                log.error(id+" log message 3");
            } catch (Exception e) {
                System.out.println("Error in "+id);
                e.printStackTrace();
            }
        }
    
        public static void main(String [] args) {
            try {
                StringWriter consoleWriter = new StringWriter();
                appender = new WriterAppender(new PatternLayout("%d{ISO8601} %p - %m%n"),consoleWriter);
                appender.setName(CONSOLE_APPENDER);
                appender.setThreshold(org.apache.log4j.Level.DEBUG);
    
                for (int i=0; i<5; i++) {
                    Thread t = new Thread(new Log4jTest());
                    t.start();
                }
    
                Thread.sleep(200);
                System.out.println(consoleWriter.getBuffer().toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    And here is my log4j.xml:

    <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
    <log4j:configuration>
      <appender name="FILE" class="org.apache.log4j.RollingFileAppender">
        <param name="File" value="./Log4jTest.log" />
        <param name="MaxFileSize" value="1000KB" />
        <param name="MaxBackupIndex" value="5" />
        <layout class="org.apache.log4j.PatternLayout">
          <param name="ConversionPattern" value="%d{ISO8601} %p - %m%n" />
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
          <param name="LevelMin" value="WARN" />
          <param name="LevelMax" value="FATAL" />
        </filter>
      </appender>
      <root>
        <level value="ERROR" />
        <appender-ref ref="FILE" />
      </root>
    </log4j:configuration>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.