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.
Related
We are using cas-client-core-3.3.3.jar for providing single sign on functionality in our application and we are trying to emit this jar library logs into our appliation logs.
Our application is a weblogic based application and we are using log4j for logging.
So to get cas-client-core-3.3.3.jar logs in our application log we have added this property in our log4j.properties
log4j.logger.org.jasig.cas=DEBUG
but we are not getting the logs which are expected from org.jasig.cas classes. I am attaching sample log here which is expected
2015-05-13 10:00:17,798 DEBUG [org.jasig.cas.client.validation.Saml11TicketValidator.<constructValidationUrl>] - Placing URL parameters in map.
2015-05-13 10:00:17,801 DEBUG [org.jasig.cas.client.validation.Saml11TicketValidator.<constructValidationUrl>] - Calling template URL attribute map.
2015-05-13 10:00:17,802 DEBUG [org.jasig.cas.client.validation.Saml11TicketValidator.<constructValidationUrl>] - Loading custom parameters from configuration.
2015-05-13 10:00:17,803 DEBUG [org.jasig.cas.client.validation.Saml11TicketValidator.<validate>] - Constructing validation url:
Disclaimer: I've never worked with Web Sphere but worked a lot with different logging systems, so my answer is based on my experience in this area.
First off, cas uses slf4j under the hood which is great.
Slf4j is only an interface (slf4j-api jar), and if you want to use it with log4j which is a concrete implementation of logging system that knows nothing about slf4j apis you should provide an implementation of sfl4j interfaces that will delegate the calls to log4j loggers.
So you should also include such an adapter in classpath as well Here is the link.
Now if this doesn't work, then probably the log4j.properties are not configured correctly, for example, the logger doesn't have any associated appenders/wrong appenders.
I've found the best way to check this is just to place a breakpoint on the logger's call (inside cas library) and see the following:
Which implementation of slf4j interface is actually used (as I've said before org.sl4j.Logger is just an interface and it has to be instantiated with real implementation object somehow, you know)
See the associated appenders to the underlying implementation.
Regarding the second item, depending on technology/frameworks you have, you might be able to get this information via JMX or some kind of web admin interface. Debugging is a "hardcore" general way to figure this out.
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.
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
Is it possible to define two separate loggers each with its dedicated file handler for one class using java.util.logging? I want separate log file for errors and other application messages. I am trying but no success so far.
Can anyone suggest what is the possible solution for this? I am initializing one logger at the start of application which reads configurations from logging.properties and creating other logger within one function of the class.
I know log4j can allow you to send messages with different thresholds (errors or application messages) to seperate files. Perhaps you could look into that.
Take a look at How to configure log4j to log different log levels to different files for the same logger
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");