How can I determine what log configuration source Logback actually used? - java

log4j has a property, log4j.debug, which will helpfully provide the user with an indication of which configuration file was actually used to configure the logging system.
I haven't been able to find anything equivalent with the (otherwise superior) Logback logging framework. Is there any way to print (for diagnostic purposes) at runtime, which configuration file Logback used to bootstrap itself?
[edit]
To clarify, I'd ideally like a solution that doesn't require me to modify the configuration file itself (since a badly assembled third-party JAR, for example, may be picked up incorrectly, and prior to my logback configuration XML).

You can set a Java system property to output Logback debugging info:
java -Dlogback.statusListenerClass=ch.qos.logback.core.status.OnConsoleStatusListener
This is further explained by the Logback documentation for automatic status printing (very bottom mentions forcing status output) and the logback.statusListenerClass property:
In the absence of status messages, tracking down a rogue logback.xml configuration file can be difficult, especially in production where the application source cannot be easily modified. To help identify the location of a rogue configuration file, you can set a StatusListener via the "logback.statusListenerClass" system property (defined below) to force output of status messages. The "logback.statusListenerClass" system property can also be used to silence output automatically generated in case of errors.

If you want to go deep into Logback, you can do the following
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.joran.util.ConfigurationWatchListUtil;
public class Main {
private static final Logger logger = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) throws Exception {
LoggerContext loggerContext = ((ch.qos.logback.classic.Logger)logger).getLoggerContext();
URL mainURL = ConfigurationWatchListUtil.getMainWatchURL(loggerContext);
System.out.println(mainURL);
// or even
logger.info("Logback used '{}' as the configuration file.", mainURL);
}
}
It will print the URL of the loaded configuration file.

you can set debug="true" in a logback.xml file that you control like this:
<configuration debug="true">
(...)
</configuration
and tho make sure that file is going to be used by logback add following VM argument when you start your program:
-Dlogback.configurationFile=/path/to/yourlogback.xml
This does not really answer to your question but gives you a work around solution.

Not very scientific, but it works if you just want a quick confirmation.
I simply changed the log entry pattern and observed whether or not it changed in my console/log file.

Related

How can I disable logging for a certain library? [duplicate]

I am using Apache Commons Logging ™. For now I wanted to use SimpleLog implementation, but when I changed the level, loggers from the libraries came out. I want it to turn them off.
Is there a easy way to change log level for whole package (can Log4j do that)?
I have tried to set
org.apache.commons.logging.simplelog.log.foo=fatal
in the property files to disable (setting to fatal is OK) foo logger, but it doesn't work (foo is a name of logger that appears in output : [INFO] foo - Message).
In Log4j you can specify a logging level for specified package, class or logger identified by string. You just simply write this in log4j.properties file:
log4j.logger.<your package> = DEBUG|INFO|OFF|WARN...
You should use:
log4j.logger.foo = OFF
Please note that "foo" does not need to be a package, or a class, but is an arbitrary String. We e.g. have a logger named "SQL" that is called from many classes.
If you use Spring Boot, you may set to OFF in application.properties file, by using logging.level.<package-or-class-name>=OFF Example:
logging.level.org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer=OFF
Reference:
https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.logging.log-levels
Use of SimpleLog from Commons Logging requires two configuration files unless you are using some system properties. The files are: commons-logging.properties and simplelog.properties.
The log level properties you have indicated should be placed in simplelog.properties like:
org.apache.commons.logging.simplelog.log.foo=warn
where "foo" is the logger name. Generally, this is the package or package and class name. In the following example, everything under the com.stackoverflow.utils package is set to info whereas com.stackoverflow.servlet.Dispatcher is specifically set to warn:
org.apache.commons.logging.simplelog.log.com.stackoverflow.utils=info
org.apache.commons.logging.simplelog.log.com.stackoverflow.servlet.Dispatcher=warn
The commons-logging.properties file should contain:
org.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog
Documentation here and here.

How to set different log levels in Eclipse Scout framework?

I'm having some trouble configuring proper logging in Eclipse Scout framework. My claims aren't that high as I only want to be able to set different log levels for different parts of my program in a configuration/properties/XML file.
The logging configuration in the config.ini of my Scout server plugin currently looks like this:
eclipse.consoleLog=true
org.eclipse.scout.log=eclipse
org.eclipse.scout.log.level=INFO
So as you can see this is the default logging configuration using Eclipse logging. It works fine for logging at a global level. The only thing I would like to do is to write something like this to set the different log levels:
packagename.ClassName=LOGLEVEL
As this is a very basic logging use case I think there must be some easy way to do this in Scout. Otherwise I would appreciate some help how to configure log4j, JUL or others for the use with Scout. The Eclipse Scout Wiki hasn't helped me so far. I created the example logger fragment to the host plugin 'org.eclipse.scout.commons' and removed the logging configuration lines from my config.ini but nothing happens. I'm also not sure where to put the log4j.poperties or how this is done otherwise.
I'm a bit ashamed for being unable to figure out such a basic problem, but would be very happy about some quick help.
I can tell you how to configure the logging if you choose the java logger (config.ini: org.eclipse.scout.log=java).
For the eclipse logger, I barely found any information at all.
Now, to configure the java (JUL) logging: You can do this in a file called logging.properties.
You can configure the logging by specifying the configuration file in your product:
Create your configuration file - say logging.properties inside the folder where your product file (for server or client respectively) is located. Typically this is in a folder named 'products'.
Open your product file and go to the "Launching" tab and specify your logging configuration file in the "VM Arguments" tab. Use the "java.util.logging.config.file" system property to do so:
-Djava.util.logging.config.file="${resource_loc:/com.yourapp.server/products/logging.properties}"
Now, you should be able to specify the log levels in your new logging.properties file:
### Root level of your application, all below are ignored
.level=INFO
### Handlers
handlers=java.util.logging.ConsoleHandler
### Handler properties
java.util.logging.ConsoleHandler.level=FINEST
### Override the logging level for certain classes
com.yourapp.server.SomeService.level=FINE
Alternatively, you can also use a class to initialize the logging with the java.util.logging.config.class option. See this wiki page for a detailed example.
Also, when building a WAR file, you might be interested in this answer.

How can I redirect a log in a NetBeans application with auto-generated logging in the catch blocks?

The application I am working with has log calls in the catch blocks auto-generated by NetBeans only and no other kind of logging:
Logger.getLogger(SomeClass.class.getName()).log(Level.SEVERE, null, ex);
What steps should be taken to redirect log output from the console to for example a text file?
I read Lars Vogel tutoriral http://www.vogella.com/tutorials/Logging/article.html and for the most part understand what he is doing, but apparently when he wanted to log an event, he would call a method from an instance of his own logger LOGGER.
NetBeans developers probably intended to auto-generate the log calls the way they did for a reason. Does their logging have to be replaced as in the tutorial, or can it be simply configured to use another logging destination?
My confusion stems from the fact that Logger.getLogger is a static method.
What steps should be taken to redirect log output from the console to for example a text file?
From the Java Logging Overview Examples:
public static void main(String[] args) {
Handler fh = new FileHandler("%t/wombat.log");
Logger.getLogger("").addHandler(fh);
Logger.getLogger("com.wombat").setLevel(Level.FINEST);
...
}
That example creates a wombat.log file in the temp directory.
Otherwise you can modify or specify a logging.properties entry to install a FileHandler. Here is a modified entry of the 'lib/logging.properties' located in your Java Home directory.
############################################################
# Global properties
############################################################
# "handlers" specifies a comma separated list of log Handler
# classes. These handlers will be installed during VM startup.
# Note that these classes must be on the system classpath.
# By default we only configure a ConsoleHandler, which will only
# show messages at the INFO and above levels.
#
#handlers= java.util.logging.ConsoleHandler
# To also add the FileHandler, use the following line instead.
handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler
You can use the 'java.util.logging.config.file' system property on launch to specify alternate log configuration files.
if getLogger() is a static method, which executes without an instance of Logger class being created,
Contained in the Java Logging Overview listed above there is a link to the Java SE API Specification. That is the contract for how all this works.
If you look up Logger.getLogger you'll see:
Find or create a logger for a named subsystem. If a logger has already been created with the given name it is returned. Otherwise a new logger is created.
If a new logger is created its log level will be configured based on the LogManager configuration and it will configured to also send logging output to its parent's Handlers. It will be registered in the LogManager global namespace.
The getLogger method is creating or locating a logger.
than where does addHandler() persist its result?
Handlers are store on the logger instance. The output type depends on the handler. The properties of each handler are described in the API spec.
The Logging Hierarchy and Record Forwarding might help you understand what is happening in the example code.

Set log4j log level

I'm currently working on a project that uses log4j.
I'm running a testcase (junit) and would like to set the log level to trace so that I can see if all the values are correct. Classes that use logging in the project contain a line like the following:
private static final Log LOG = LogFactory.getLog(MatchTaskTest.class);
and use a like like this to do the actual debugging
LOG.trace("value");
I have never used log4j before, does anybody know how I can change the log level just for the testcase, preferably simply by defining a parameter in eclipse's run configuration dialog.
Using another configuration file
Perhaps you could point to another configuration file.
java -Dlog4j.configuration=config file yourApp
Where:
config, you file of configuration, e.g. log4j.properties or log4j.xml.
file, the log file, e.g. myApp.log
yourApp, you app, e.g. MyAppGUI
Or you can use a class
java -Dlog4j.configurationClass=config class yourApp
Where:
config, you file of configuration, e.g. log4j.properties or log4j.xml.
class, any customized initialization class, like LogManager, should implement
the org.apache.log4j.spi.Configurator
yourApp, you app, e.g. MyAppGUI
You can see more in Apache log4j 1.2 - Short introduction to log4j on Default Initialization Procedure section.
Modifying the level programmatically
Moreover, you can also use the methods that offers the Logger class, like public void setLevel(Level level), e.g.:
Logger.getRootLogger().setLevel(Level.TRACE);
Since you want only for testing purposes, you could use them. But it is recommended not to use in client code because they overwrite the default configuration parameters in hard coded. The best way is to use an external configuration file.
In your junit class put:
Logger.getRootLogger().setLevel(Level.TRACE);
somewhere before the execution of the tested method. It will set the threshold level of the root logger to TRACE.
If you're using Maven, you can have two log4j configuration files:
one in src/main/resources, containing your production logging config
one in src/test/resources, containing your test-time logging config
Maven will automatically use the latter at test time, and bundle the former into your artifact (JAR, WAR, etc) so that it's used in production. You don't have to mess around with command line switches or anything.
I don't think this is possible.
The config file is going to let you configure what log messages actually surface in the log, not what level each message is going be logged at. This makes sense - the config should not affect the level of the message.
The javadoc has a method for each log level and a generic log method, which takes in a priority, so I'm not sure there's even a default to be set.
You can set a config file explictly on the command line via -Dlog4j.configuration=<FILE_PATH>, so you could set up a specific config for that test case.
I have no idea why some of the above didn't work for me. (I don't want to write config file). following works for me
Logger log1 = Deencapsulation.getField(Some.class,"logger");
log1.setLevel(Level.DEBUG);
NB that the log4j2.properties file may include the line
filter.threshold.level = debug
You can waste an entire afternoon trying to figure out why your LOG.trace() statements aren't outputting anything!
I actually put it in the #Before method. But I believe it could (and should) be placed in the #BeforeClass.
If you set it in the class body you'll get compiler errors.

slf4j logging with jdk – how to enable debug?

By default slf4j, when using with jdk (slf4j-jdk14-1.6.1.jar), does not log debug messages.
How do I enable them?
I can’t find info neither in the official docs, the web or here on how to enable it.
I found some info on (failed though) creating a file in %JDK_HOME%/lib and defining the level there, in a config file.
However, I would like to define the level at compile-/run-time so I can both run and debug my app from my IDE with different logging levels.
Isn’t there some environment variable I can set, or VM arg?
Why do you think it does not log DEBUG messages?
If you mean that your log.debug(String) logging calls do not end up in java.util.logging log files, then I guess you have to configure the logging.properties configuration file to allow log messages at FINE level.
If you do not want to mess with the global %JRE_HOME%/lib/logging.properties, then you can just pass in -Djava.util.logging.config.file=logging.properties on the command line - this will force the logging system to look for that configuration file in the current directory.
Or use some other (programmatic) way to configure java.util.logging, see below for tutorial.
This has nothing to do with configuring SLF4J; in fact, SLF4J does not have any configuration, everything is configured by simply swapping JAR files.
For your reference:
JDK14LoggerAdapter
Java Logging API tutorial
If you are using slf4j SimpleLogger implementation read this.
There you can see that simpleLogger use INFO as default log level. You can change it by using a system property. This is usefull for non-production evironments:
static {
System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "trace");
}
You can add -Dorg.slf4j.simpleLogger.defaultLogLevel=debug to the VM options.
I just put my logging.properties file in my applications WEB-INF/classes file (or use the command line argument identified by Neeme Praks if you're not deploying in a war), and have the properties file open in eclipse so I can fine tune it to log the packages and at the level I'm interested in.
In the logging.properties file, you need to make sure that both the logger level and the handler level are set to the level you want. For example, if you want your output to go to the console, you need to have at least the following:
#logging.properties file contents
#Define handlers
handlers=java.util.logging.ConsoleHandler
#Set handler log level
java.util.logging.ConsoleHandler.level=FINE
#Define your logger level
com.company.application.package.package.level=FINE
#Assign your handler to your logger
com.company.application.package.package.handlers=java.util.logging.ConsoleHandler
You mentioned the slf4j-jdk14-1.6.1.jar. This provides the slf4j binding to java.util.logging. You need to have that in your classpath, but make sure you also have the slf4j api (slf4j-api-1.7.12.jar) in your classpath as well.
I find the example logging.properties file in this link useful for creating a variety of loggers and handlers, to give you fine-grained control over what logs go to the console, and what logs go to a file:.
And here's the slf4j manual.
if you are using lombok Slf4j
package com.space.slf4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* #author anson
* #date 2019/6/18 16:17
*/
#Slf4j
#RestController
public class TestController {
#RequestMapping("/log")
public String testLog(){
log.info("######### info #########");
log.debug("######### debug #########");
log.error("######### error #########");
return null;
}
}
application.yml
logging:
level:
root: debug
In runtime with the default configuration you can enable it with this code:
public class MyTest {
static {
Logger rootLogger = Logger.getLogger("");
rootLogger.setLevel(Level.ALL);
rootLogger.getHandlers()[0].setLevel(Level.ALL);
}
I'm using this code inside TestNG Unit.

Categories