Log4j Logging to a Shared Log File

Is there a way to write log4j logging events to a log file that is also being written to by other applications. The other applications could be non-java applications. What are the drawbacks? Locking issues? Formatting?

Asked by: Marcus431 | Posted: 28-01-2022

Answer 1

Log4j has a SocketAppender, which will send events to a service, which you can implement yourself or use the simple implementation bundled with Log4j.

It also supports syslogd and the Windows event log, which may be useful in trying to unify your log output with events from non-Java applications.

If performance is an issue at all, you want a single service writing the log file, rather than trying to coordinate a consistent locking strategy among diverse logging applications.

Answered by: Alberta886 | Posted: 01-03-2022

Answer 2

Your best bet might be to let each application log separately, then put a scheduled job in place to 'zipper' the files together based on time. If you need really up-to-date access to the full log, you could have this run every hour.

Answered by: Ada252 | Posted: 01-03-2022

Answer 3

I have a experience with the following two approaches:

  1. Use database for logging instead of plain text file - it can be prohibitive because of performance issues, on the other hand it is very easy to analyze logs, create reports. Database takes care for all concurrency problems.
  2. The other approach involves usage of JBoss server, which can be used to read logging information from other sources. JBoss can be run in the minimal configuration and thanks to that it is really lightweight (2 seconds startup time). Details can be found here http://docs.jboss.org/process-guide/en/html/logging.html (Logging to a Seperate Server). Log4J takes care of all locking/concurrency issues.

If you are not planning to use JBoss you can use the second approach as a base of your own logging solution.

Answered by: Lenny926 | Posted: 01-03-2022

Answer 4

I don't think the default log4j appenders do any file locking or synchronization. Without such locking, you're likely to lose log messages or receive them mangled.

I'm not sure how easy it is to do file locking in Java, but to make this work directly, I think you would need to subclass the appropriate Appender and override the logging method, wrapping it with synchronization code that locks and unlocks the file. This might have performance implications, depending on your system load.

Log4perl has a synchronizing Appender, Log::Log4perl::Appender::Synchronized, which wraps an Appender and accomplishes this, and also seems to provide a flag in its Log::Log4perl::Appender::File that prevents interleaved lines. You might look at what those do to see if it could be replicable in Java.

Answered by: Kelsey265 | Posted: 01-03-2022

Answer 5

Log4j is flexible enough that you can create log entries in a format that is compatible with the records already in the file. Just look into the appenders for how to format specific data fields.

Your main concern will be with locking, most likely by other applications. Make sure all of the applications do not have an exclusive lock on the file and you should be fine.

Answered by: Lenny868 | Posted: 01-03-2022

Similar questions

java - What logging can be used to log in GWT shared code?

There are three types of code in a GWT project: Client code (GWT client / javascript) Server code (servlet container / java) Shared code (GWT client & servlet container / javascript & java) Logging framework usable for each type: Client - gwt-log Server -

logging - Java RMI tracing

Is there a tool which traces & logs all RMI activity of a Java application?

What's Up with Logging in Java?

java - Logging user actions

The customer wants us to "log" the "actions" that a user performs on our system: creation, deletion and update, mostly. I already have an aspect that logs the trace, but that works at a pretty low level logging every method call. So if a user clicked on the button "open medical file" the log would read: closePreviousFiles("patient zero") createMedicalFile("patient zero") --> file #001 chang...

How do I unify logging formats from the java stack

Using a Jetty web server, started from maven, which includes iBatis, Spring, Jersey, a little of this and a little of that, I get logging output with a host of formats. Some are from maven: [INFO] [war:war] [INFO] Exploding webapp... Some are from Jetty: 2009-03-25 21:01:27.781::INFO: jetty-6.1.15 2009-03-25 21:01:28.218:/example:INFO: Initializing Spring root Web...

logging - How to get java Logger output to file by default

This question already has answers here:

logging - Java Logger only to file, no screen output

I have quite a simple problem but can't find a solution for it. I have a logger with a file handler added, but it still spams the hell out of my console. How could I get the logger to solely route all output to a file, with NO console outputs?

How to enable FINE logging for a single java class

I'm using java.util.logging.Logger logging in my program. How do I enable FINE logging for a single class, while setting it to WARNING for every other class? I'd prefer to do this programatically in my main() method rather than needing to set up ...

java - Turn off tomcat logging

I want to turn off tomcat's logs jvm.stderr and jvm.stdout which have been set in the wrapper.properties, I've commented these lines out but that just redirects the logs to be written to the root tomcat folder. The reason for turning them off is that these logs do not seem to have any sort of size control so I have a situation where they are getting too big and causing problems. Is it possible to totally d...

java - How do I turn logging off using log4j?

I am using a third-party library which has a log4j.xml configuration - what's the best way to turn off the logging?

logging - In Java, how can I include dynamic version information in a log file?

From a Java application I can log a string, using a custom logging framework, as follows: logger.info("Version 1.2 of the application is currently running"); Therefore when a user sends me a log file I can easily see what version of the application they are running. The problem with the code above is that the version is hardcoded in the string literal and someone needs to remember ...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)