java 多线程委托,在java中登录多线程应用程序

Bertha 。 2023-01-24 04:24 19阅读 0赞

![Image 1][]

What’s the best way and best tool for logging in multi-threaded environment, so that each thread has it’s own logger instance and separate file. is this even possible?

解决方案

You may try using a custom Log4J appender, which takes thread id as a parameter and filters messages based on what thread calls it. Create it on the fly, attach it to the logger.

There are multiple problems with this approach though:

Too many appenders would slow down the logging

AppServers usually have a thread pool. That means that the same thread over time would take part in executing totally unrelated requests, which will end up in the same log file.

I suggest you consider a simpler approach: log thread id into the same log file. It’s fast and simple, log4j has a % flag for doing it. Later you can grep/split the log file per thread id if required.

Update:

Actually, you may have a single custom appender, which will open log files on demand when a new thread logs a record (the appender is executed within that thread context, just call Thread.currentThread().getName()). But you’ll have to re-implement all usual log file tasks (rotation) or delegate it to standard appender for each file.

[Image 1]:

发表评论

表情:
评论列表 (有 0 条评论,19人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java 线-线通信

    最近,美美非常的爱吃栗子,剥栗子却有些麻烦,这个任务理所当然的交给了帅帅,每一次,帅帅都会把热气腾腾的栗子剥好,然后放进一个盘子里,而美美每次都会从盘子里拿一个栗子吃: !