I have an existing web application which uses log4j for logging purposes and it will create a log file in my local system. Now, I have to migrate that web application to AWS platform. In order to move it to AWS, what changes need to done in terms of logging? How should be the log4j.properties file? Is there any need to change the application code?
No changes are required to your application; log4j will still work.
You do not want to fill up the file system with logs, but you also want to keep an appropriate amount of logging in case you have an issue you need to debug.
Log4j will handle its own log rotation. You will want to make sure your log4j.properties file is set up to keep logs for a certain length of time.
Make sure your logging directory exists on the new server and/or create it with the correct permissions. Or change the log4j.properties file.
log4j.appender.R.File=/some/dir/logs/your_app.log
Related
So basically i have a dropwizard project which uses logback by default,but i want to use log4j in my project.The log files i want to create are access and application logs.so basically application log will contain the overall logs and the access log will contain just logs for access of that service.
Now in logback i can do this directly using the logback-access jar and defining the required configs in config.yaml file.So my question is how exactly i can achieve this in log4j,like is there something available similar to logback-access in log4j or would i have to write an appender for it,if yes how exactly.
Thank you for your answers in advance
I read a lot but I couldn't figure out how I could specify for example the log level for specific classes.
Only way I could figure out was in the standalone.xml but why should I configure some application specific setting very general in the server? This complicates the deployment process unnecessary.
Isn't it somehow possible to define the specific log level and the output files somewhere inside the war without touching the server?
Btw. it doesn't matter if log4j or commons-logging or slf4j or whatever is used.
Using a logging.properties file or a log4j configuration file in your deployment will work in JBoss EAP 6.x and WildFly (formerly JBoss AS). Note though that a log4j configuration would only work if you use log4j for your logging facade.
That said I agree with Marko that this should probably be done in the server configuration. I would also encourage you to use the CLI or web interface rather than editing the raw XML as well. You can get some more information on the documentation.
I am sorry for not providing a direct answer, but consider this: the application being in charge of logging levels is a bad idea most of the time as this is something an AS admin should be able to change at any time. For example, the point of the DEBUG or TRACE log levels is to be able to place a lot of such statements in the code without hurting the production server's performance. However, once a bug is detected, you want to be able to lower the logging level without rebuilding the application. This should be a purely administrative task.
On the other hand, I do recognize the need to at least have a decent starting point for the logging configuration and I don't know of any architecture which would allow the application to provide defaults which are overridable by the server configuration.
Ideally Logback would have an option to always rotate on app startup. since that feature is absent, how can I force logback to rotate on app startup? Since its vendor code, I'll have to write my own program RotateLB.java. How can I do it yet use the same logback.xml the vendor app uses. It is doing rotate when log hits 250MB.
This can be done by writing your own trigger, deriving ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP and overloading isTriggeringEvent().
But you will need to edit the logback.xml config file.
If not possible, you can specify an alternate config file with -Dlogback.configurationFile=...
Or, since your app is probably using the SLF4J API, you could try to remove logback and use Log4j 2 instead, which does have the option you need.
I have multiple applications using log4j for their logging, each loading settings from separate log4j.properties loaded from the classpath (just a file on disk, not in a .jar or anything that would make it read-only). In each of these applications, I want to allow updating the log level through the regular UI.
I use a common utility class for this, which is shared between applications. I can do that for the lifetime of the app, but since the changes aren't persisted back out to the appropriate log4j.properties file, on next restart, it's reset to the specific level in the properties file.
Since it's just loaded from the classpath, I don't know the full path to the logfile in each application, and since it's a common class, I'd rather not hardcode paths. Is there a way to get log4j to write back its changes to its on-disk configuration?
I suggest you flip the problem on its head.
Always write changes to the log4j.properties file on disk, and then install the listener that checks for changes in that file and updates the appropriate log levels.
I have some jar files that will be distributed to clients that are using log4j for logging. My question is should I include a log4j.xml configuration in the jar file or have the client provide one if they want logging?
My feeling is to leave the log4j.xml configuration file out of the client jars, since the apache jar files all come with log4j logging, but sans log4j.xml.
Yes, leave it out. It's an utter nuisance when your log4j configuration file is ignored because one of the 60 third-party libraries of your app contains its own.
The good thing about log4j in your case is that your jar really shouldn't have to worry about it. The basic use case of log4j is:
Obtain a logger object for the current class
Call one of the methods on that logger, such as debug("some message");
If the jars you are shipping are to be used by a larger application, then ideally your code will only do the two steps listed above. In this way, your code will simply obtain logger objects from the already-configured log4j instance in the client's application. Your production code is then decoupled from having to know how to configure log4j.
Any logging you need to see for your development of the jars can be accomplished by configuring a log4j instance in unit test setUp() methods or something similar that won't get bundled with the production code going to the client.
I would put a default log4j configuration that you expect will be useful to your clients in the documentation. This way interested people can see what logging options you have (usually certain classes have more interesting log messages, from the user's perspective). I find it annoying when I have a third-party lib using log4j and it has no documentation, and there are log messages filling my screen and I have to try to figure out how to enable or suppress certain log messages.
If you are using log4j in your application then you include it in your project. If you are not, then why would you put it in there? What if client A wants log4j version 1.2 and client B wants log4j version 1.3.
Let them decide what they need for their projects and worry about what you need for yours.
I would add the configuration xml and load it up with instruction for the user showing different configuration and options. This will make it easier for either them or support to enable addition logging.