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 :-)
Related
I have a method foo which is called inside a loop over a list of objects.
class Bar{
private static final Logger logger = LoggerFactory.getLogger(Bar.class);
void foo(A obj){
//bunch of debug logs
logger.debug("A");
logger.debug("B");
//..and so on
//processing functionality
}
}
Now, whenever the object is processed successfully, the debug logs are printed in the log file. But if an object throws an error while processing, the debug logs are not getting printed.
I am using log4j2 and my log4j.xml file is below:
<Configuration monitorInterval="60" status="WARN">
<Loggers>
<Logger name="com.abc" level="INFO"/>
</Loggers>
<Loggers>
<Root level="INFO">
<AppenderRef ref="console"/>
<AppenderRef ref="asyncFileLogger"/>
</Root>
</Loggers>
</Configuration>
What could be the issue behind this?
I have a Spring Boot application with the following setting in logback-spring.xml
<logger name="org.hibernate.sql" level="DEBUG" additivity="false">
<appender-ref ref="CONSOLE"/>
</logger>
But this doesn't log the generated SQL unless I change sql to uppercase like this
<logger name="org.hibernate.SQL" level="DEBUG" additivity="false">
<appender-ref ref="CONSOLE"/>
</logger>
which then produces log statements like this
...org.hibernate.SQL : select count(disabled0_.user)
Also worth noting is that IntelliJ doesn't link to anything when I hover over SQL in the logger name. It links to the package when hovering overorg.hibernate but stops at SQL. But it does link to other more specific classes like this one
<logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="TRACE" additivity="false" >
<appender-ref ref="CONSOLE"/>
</logger>
in that one, hovering over BasicBinder does link to the class, and results in statements like
o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [VARCHAR]
Why does SQL have to be uppercase to work?
I am using spring-boot-starter-data-jpa:1.5.10.RELEASE
SQL has to be in uppercase, because the logger name is case-sensitive. See this answer.
IntelliJ can't link to the class, because it's not actually a class, just the name of the Hibernate logger. By convention, logger names are the class name, but that's not a requirement, they can be any string.
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.
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
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>