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>
Related
I want to remove the unnecessary log's that are generated automatically by log4j when i run my suite, I only want the log's which i wrote in the code, please help.
As you can see my actual log's starts from Launched flipcart.com and end with Closed the current window
Also check the log4j2.xml file.
See second answer in How to exclude a single Class from a Log4j Logger / Appender?
Replace
<logger name="com.example.FifthClass">
<level value="FATAL"/>
</logger>
with e.g.
<logger name="io.netty">
<level value="FATAL"/>
</logger>
I am upgrading from log4j1.x to log4j-2.8.2
I am trying to convert my existing log4j.properties file to equivalent log4j2.xml.
My first question is can I write log4j2.proeprties file instead of log4j2.xml file?
I found some equivalent tags to log4j.proeprties file in log4j2.xml.
I did not find tags for the below lines. can anybody suggest on this?
# LoggerFactory for ESAPI to utilize Log4J
log4j.loggerFactory=org.owasp.esapi.reference.Log4JLoggerFactory
log4j.category.Default=ALL, CONSOLE, RollingFile2, edelivery
log4j.rootCategory=OFF
log4j.logger.org.apache=INFO
log4j.logger.IntrusionDetector=ERROR
log4j.logger.org.springframework=WARN
Thanks in advance.
What i am sure is:
See the last three lines:
log4j.logger.org.apache=INFO
log4j.logger.IntrusionDetector=ERROR
log4j.logger.org.springframework=WARN
I think in log4j.xml they should be instead of like these below:
<Logger name="org.apache" level="INFO"/>
<Logger name="IntrusionDetector" level="ERROR"/>
<Logger name="org.springframework" level="WARN"/>
And what I am not sure is:
these two lines:
log4j.category.Default=ALL, CONSOLE, RollingFile2, edelivery
log4j.rootCategory=OFF
I guess you have configured some appenders with the name in above line like:
<appender name="CONSOLE">
other proerties....
</appender>
so i think you could try to use these in your log4j.xml:
<root>
<appender-ref ref="ALL" />
<appender-ref ref="CONSOLE" />
<appender-ref ref="RollingFile2" />
<appender-ref ref="edelivery" />
</root>
And What I really don't know is:
log4j.loggerFactory=org.owasp.esapi.reference.Log4JLoggerFactory
I Know nothing about esapi so i could not suggest how to deal with it, hope someone else to give more answer.
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
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 :-)