LOG4j - ways to configure it - java

I'd like to configure log4j in a huge distributed system. There are a lot of JVMs and processes and (as I am only a student) I am new to such a situation. I use JMS Appender, so I need two files: log4j.properties, jndi.properties.
I've tried some ways:
Put these files into src folder
or into src/java
add jvm argument -Dlog4j.configuration=C:\...\log4j.properties
I don't want to set it in code using PropertyConfigurator.configure("path"), because I'd like to not change source code at all.
Are there any other ways to configure log4j? Or maybe I am missing something? I still get a messages
No appenders could be found for logger (some.package.SomeClass).
Please initialize the log4j system properly.

basically you have the following ways to configure Log4J:
Via your code
properties file (as you're trying to use)
Xml File
What you're asking to me related to specific configuration of JMS appender and its not about 'general way of how to configure log4j'
Here is an example of how to configure it with ActiveMQ
For other JMS implementations in your property file the keys will be probably the same but the values will be different
Example - using JMS appender with ActiveMQ

Related

How to choose log4j2 appender at runtime

I use log4j 2.9
I have file appenders configured in log4j2.xml.
For example :
all_log_appender, device_appender, sql_appender
all appenders log in different files.
what is a correct way to choose my appenders at runtime in order to decice where (in which file) to log?
To be honest I can't find examples in the documentation. I also tried to extend Logger class but as I understand it's not correct to extend log4j2 logger class because "some layouts require Log4j to walk the stacktrace to report which class and line in your code from the logger was called from"
I can't find any examples how to choose appender at runtiime.
Could you give me a simple example please?
This can be accomplished in configuration (no custom code required) by using the Routing Appender built in to Log4j2.
This appender can route log events to a set of predefined appenders or it can dynamically add new appenders if necessary. This stackoverflow answer has examples and links to other examples.
The manual page has three examples, but the FAQ page ("How do I dynamically write to separate log files?") has an example that uses the ThreadContext map to control which log file subsequent events (in the current thread) get logged to. (See also this example on StackOverflow.)
Instead of the ThreadContext, it is also possible (see LOG4J2-1015) to route based on log event Markers, so the application can specify on a per-event basis which file appender the event is routed to. See this StackOverflow answer for an example that routes based on markers.

How to configure IBM WAS SystemOut logging to customize logging for each application?

I have 4 appllications (ear) on my WAS. I need them to write in SystemOut.log some sign. I.e each application must write to log file its own sign. For example:
[16.01.17 3:50:05:592 GMT+05:00] ADMIN 000005e0 SubsystemMess I com.docflow.core.integration.jms.SubsystemMessageListener onMessage_aroundBody0 Subsystem integration message ID:f5392a5ec3b3f41502095b00110a134f0000000000000001 of type DP_EKS_BANK_GUARANTEE_RECEIVED process finished
Here ADMIN is sign of the application.
How i can do that?
It's not possible to adjust the default logging format in this way. Your options would be to use java.util.logging.Logger with a particular name (to replace the "SubsystemMess" part) or to use a separate logging package and configure it to log to a separate file.
Look at log4j and configuring each app's log4j to go to a different file.
Log4j's file appender can be configured to also include timestamp and more.
The only catch is you can't configure it from WAS's admin console then.
Note that java.util.Logger writes to the trace.log. If you use a different logger name, it will list as such in the same trace.log file. Upside is you can now configure it from the admin console.
One potential solution could be to switch to HPEL logging. Then you could query log entries for your given application using logviewer, like:
logViewer.sh -includeExtensions appName=PlantsByWebSphere
I know that it is not exactly what you are looking for, but maybe it will be sufficient for your needs.

Vert.x: best way to log to file

What is the fastest async way to log to file in Vert.x?
The aim is to write logs from loggers from different classes (i.e. Class1, Class2 etc) to 1 file (something like 'console.log')
Vert.x uses the JDK bundled JUL logging framework to avoid shipping additional dependencies. However it allows to append a custom logger implementation.
Assuming that you want to stick to the default logging facility, customizing the log handler would then be as easy as droping a logging file and referencing it through the java.util.logging.config.file system property:
For example you can drop the logging configuration file under a config directory under the root path of your (fat) jar which may look as follows:
handlers = java.util.logging.MyFileHandler
config =
#...
You should then refrence that file in a system property as follows when starting your Vert.x application:
-Djava.util.logging.config.file=config/logging.properties
You can then access the Logger object in your classes as follows:
Logger logger = LoggerFactory.getLogger("some.package.MyClass");
Use that logger to log messages that will be handled by the configured handler(s):
logger.info("some informative message");
Note the use of a custom log handler in the properties file to emphasis the possibily of appending your own handler (which may extend the default FileHandler).
Check the Vert.x documentation for more informations on how to use explore the logging feature.
Most of loggers are async from the beginning , i.e. they are not write information immediately. Logs are stored into buffer, which is flushed by timeout or when it is full. So slf4j + log4 is good enough for most cases.

Multiple log4j.xml in a jar files

I am developing a client api where I have a specific requirement to log the client api specific log messages to a separate file and it is pretty straight forward: I created an appender and associated the appender with logger which is specific to my package.
Now the question is:
What happens if the client application has its own log4j.xml? How will my appender and logger work in that environment ?
The log4j initialization process only handles one configuration file - thus this file should contain all logging configuration that should be active. You probably have to define a specific configuration for the client application that contains your and the client applications logging configuration.
The client application has to be configured to use this file for the initialization. This is done by setting the log4j.configuration system property as described in the log4j manual (suppose that you are using log4j 1).

hsqldb internal event log configuration

How do I configure internal event monitoring in hsqldb? When I run my Java application, I get the following warnings:
log4j:WARN No appenders could be found for logger (HSQLDB2C7984E18B.org.hsqldb.persist.Logger).
log4j:WARN Please initialize the log4j system properly.
The documentation tells me log4j is not the only option, but it doesn't tell me how to configure my application. Can anyone point me to this documentation? Remember, I don't want to use log4j for hsqldb.
It bears mentioning that a 3rd-party jar I'm referencing requires log4j. Does hsqldb automatically detect that log4j is present and then attempt to use it? Or am I missing something fundamental about how logging works?
Check out this link. It says
The logging facility hands off to Log4j if Log4j is found in the classpath, and otherwise will hand off to java.util.logging.
The consequence of what the message indicates is that no logging for HSQLDB will take place because no appenders were found.
If you wish to suppress the message, add a line like the one below to the log4j.properties file:
log4j.logger.HSQLDB2C7984E18B.org.hsqldb.persist.Logger=FATAL
This will log only FATAL events, which wouldn't happen in normal operation.
You also state that you don't want to use log4j for HSQLDB. Software components that can use log4j leave the logging configuration (including level and where to log, etc.) to the log4j properties settings, which you can edit and configure.
In this case, the logger name is based on the "unique" database name which is initially autogenerated, but which you can change in HSQLDB.
Because as YWE noted hsqldb uses log4j by default if it is found in the classpath, I needed to figure out how to override the log4j.properties found in the 3rd-party library. This I managed to do as follows:
Copy the existing log4j.properties to my project, and add the following line at the beginning:
log4j.rootLogger=WARN, CONSOLE
Add the following VM Arguments:
-Dlog4j.log4j.defaultInitOverride=true
-Dlog4j.configuration=C:/full/path/to/my/log4j.properties
Make sure this line of code runs before anyone (e.g. hsqldb) attempts to use log4j:
org.apache.log4j.PropertyConfigurator.configure("log4j.properties");

Categories