Allow future developers block my logger - java

I am using logback for logging and have no logger set, just root logger:
<root>
<level value="ALL" />
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
In class it looks like this:
Logger log = LoggerFactory.getLogger(foo.bar.MyClass.class); // foo.bar is package definition
Can future developers block my logging by defining rules for my packages, like this:
<logger name="foo.bar" level="OFF" />
Or, I must define my logger in class like this:
Logger log = LoggerFactory.getLogger("MyCoolLogger");
And people can disable it thanks to given name:
<logger name="MyCoolLogger" level="OFF" />

Yes.
Logback loggers inherit settigns from their parent loggers, so foo.bar.MyClass will inherit settings from foo.bar

Related

Ways to configure logs other than log4j xml, properties file, and source code (mainly java)?

I'm looking at the source code of some application. It is using Spring framework, Apache Tiles, JSP, Log4j, java, javascript, jquery, jqplot, Jsch, and etc.
I know where the logs are created. (a/b/logs) However, when I look at source code, I don't know how logs are created under the folder name 'logs'. I looked at log4j.xml, web.xml , property files. I found the code for how the path 'a/b' is created, but not logs. Also that folder has 4 types of logs. And they are name in a pattern like access.20181227001 , errors.20182111. I want to know where I have to look to find how the logs are created in this manner.
Log4J.xml
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %5p [%c] %m%n" />
</layout>
</appender>
<appender name="console-infolog" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %5p %m%n" />
</layout>
</appender>
<!-- Application Loggers -->
<logger name="com.dsmentoring.chakan" additivity="false">
<level value="debug" />
<appender-ref ref="console"/>
</logger>
<!-- 3rdparty Loggers -->
<logger name="org.springframework.core">
<level value="error"/>
</logger>
<!-- Bean logger -->
<logger name="org.springframework.beans">
<level value="error"/>
</logger>
<!-- Context logger -->
<logger name="org.springframework.context">
<level value="error"/>
</logger>
<!-- Web logger -->
<logger name="org.springframework.web">
<level value="error"/>
</logger>
<logger name="org.springframework.ldap" additivity="true">
<level value="error"/>
</logger>
<!-- LDAP logger -->
<logger name="com.unboundid.ldap" additivity="true">
<level value="error"/>
</logger>
<!-- Root Logger -->
<root>
<priority value="off" />
<appender-ref ref="console" />
</root>
To sum up :
1) Is there a way to configure where logs are created and how they are created (4 types of logs) Other than log4j.xml, xml files and property files? I looked at all the java, jsp, js code but can't seem to find the configuration for logs. So I want to know if there are other ways to do that or where I should look for those configuration.
2) The 'logs' folder is possibly default for log4j?
ldap.properties
#LDAP Connection Info
ldap.host=192.168.0.17
ldap.port=22389
ldap.userName=cn=directory manager
ldap.password= 9074B18A0DE2D50C068D37B60BE5DFDE
ldap.baseDN=o=sso30root
ldap.defaultLoadSize=1000
ldap.start=start-ds
ldap.stop=stop-ds
ldap.workdir=/home/KB_openDJ // logs are created under this path
// /home/KB_openDJ/logs
In other java class, they use this.
#Value("${ldap.workdir}")
private String WORK_DIR;
// I ommited many lines in between
try{
diskUsage = sigar.getFileSystemUsage(WORK_DIR);
diskIOInfo.setDiskRead((int)(diskUsage.getDiskReadBytes()));
diskIOInfo.setDiskWrite((int)(diskUsage.getDiskWriteBytes()));
}catch(SigarException sigarEx){
log.debug("Disk Usage info load Error : " + sigarEx.getMessage());
}
I used the 'Search' feature in Eclipse many times. ( logs, WORK_DIR, and 4 types of log name, and many others. I am unable to locate the code about logging configuration. :(
My log4j version :
1.2.15
Ok, it seems that there's a solution which might help you. It requires some debugging of static initalization block of org.apache.log4j.LogManager class. This class is responsible for loading logger configuration file. Here's a documention link which thoroughly describes initalization process: link.
Here's an excerpt from a LogManager source file:
String configurationOptionStr = OptionConverter.getSystemProperty(DEFAULT_CONFIGURATION_KEY, null);
String configuratorClassName = OptionConverter.getSystemProperty(CONFIGURATOR_CLASS_KEY, null);
What I'm trying to show you here, is that your logger configuration file might be specified as a JVM option supplied to your application-server. That's why you are not able to determine actual file being used.
Even if this approach fails, I'd recommend you to investigate the appenders list retrieved at run-time. Here's a stackoverflow thread which describes how to do so: link.

How to write all log output to file spring boot

I'm using logback-spring.xml in a spring boot project. I want to log all uncaught exceptions to a file. Basically just direct the output from the console to a file. I've tried several variations of logback-spring.xml.
I tried this
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<logger name="com.mine" level="WARN" additivity="false">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</logger>
<root level="INFO">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE">
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
<resetJUL>true</resetJUL>
</contextListener>
</appender-ref>
</root>
</configuration>
with this in my main method SLF4JBridgeHandler.install();
I've tried this from the spring docs
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</configuration>
this is in my application.yml
logging:
file: C:\dev\assessment-tool\logs\application.log
config: classpath:logback-spring.xml
For these it will log all the start up logging messages but when an exception is thrown and not caught it goes to the console and doesn't show up in the log file. How can I make uncaught exceptions go to a log file?
The problem is on the levels you're assigning to the loggers in the logback-spring.xml file.
According to the logback architecture documentation, (you can check it here) "The effective level for a given logger L, is equal to the first non-null level in its hierarchy, starting at L itself and proceeding upwards in the hierarchy towards the root logger".
You're assigning level "INFO" on your root logger, and level "WARN" on the logger "com.mine". None of them get "called" when there is an exception, ex: logger.error(Exception e).
One solution is assign the level "DEBUG" or "ERROR" to the logger "com.mine" or to the root logger, because they have attached the appender "FILE".

What is the result of making log4j additivity equals to true or false?

In simple terms what is the result of making additivity="true" or additivity="false" when adding a Log4j configuration for a specific class like this?
<Logger name="com.mypage.glass.TryWindow" level="INFO" additivity="true">
<AppenderRef ref="console"/>
<AppenderRef ref="file"/>
</Logger>
By default, a logger inherits the appenders from its ancestors. By setting additivity="false", you prevent this behaviour.
In your example, there may be appenders associated with com.mypage.glass or com.mypage or even the root logger that would be inherited if you don't set that property to false.

Logback - using appenders per method, rather then class

I have a class with several methods. I would like each method to output to a different log file. With a logback.xml file like this, it logs ALL logging calls from the class.
<logger name="com.mincom.ellipse.conversion.BatchConverter" level="INFO">
<appender-ref ref="FILE" />
</logger>
How do I get per method logging calls. I'm sure it's very simple, but I cannot seem to see the answer in the doco.
Haven't used logback, but in log4j and others you can setup loggers with any name you like. Using the classes package and name is just a convention. So I'd setup Multiple loggers in your class, something like this:
Logger logA = LogFactory.getLogger("LogA");
Logger logB = LogFactory.getLogger("LogB");
public void methodA() {
logA.debug(...);
}
public void methodB() {
logB.debug(...);
}
And then in your logback setup ...
<logger name="LogA" level="INFO">
<appender-ref ref="FILE-A" />
</logger>
<logger name="LogB" level="INFO">
<appender-ref ref="FILE-B" />
</logger>
Should work. Probably needs some tweaking :-)

log4j: logging a package, but excluding its sub-packages

Hi I'd like to exclude certain subpackages from being logged by one of my loggers, as they are being logged by another logger. Eg.
com.mysite.app = logger1
com.mysite.app.news = logger2
com.mysite.app.events = logger3
I'd like logger1 to only log anything with com.mysite.app (including com.mysite.app.utilities) not logged by logger2 and logger3. How could I do that?
(help in properties format please, XML format for other's reference for bonus points)
I always used to think that log4j.logger.com.mysite.app = logger1 takes care of logging messages from subpackages too into logger1.
If you really don't want logger2 and logger3's messages from interfering with those of logger1, you need to set their additivity to false.
log4j.additivity.com.mysite.app.news=false
log4j.additivity.com.mysite.app.events=false
Have a try:
log4j.logger.com.mysite.app=info, stdout
log4j.additivity.com.mysite.app=false
log4j.logger.com.mysite.app.news=off, stdout
log4j.additivity.com.mysite.app.news=false
log4j.logger.com.mysite.app.events=off, stdout
log4j.additivity.com.mysite.app.events=false
For XML configuration:
<logger name="com.mysite.app.news" additivity="false">
<appender-ref ref="logger2" />
</logger>
<logger name="com.mysite.app.events" additivity="false">
<appender-ref ref="logger3" />
</logger>

Categories

Resources