I have a a proverbial Log4J logger.
Logger logger = new Logger(MyClass.class);
Can I check that the correct filed is pass to the Logger?
Not with Log4j 1 by itself. With Log4j 2 you could just do:
Logger logger = new LogManager.getLogger();
Or you could use Lombok and just use #Log4j (or #Log4j2) at the beginning of your class.
You can't: ArchUnit does not analzye method call parameter values, see also #596.
Related
I’m remediating from log4j1.x to Log4j2.x and trying to convert below to Lg4j2.x compatible.
Actually below code is used in Test cases
#Mock
private AppenderSkeleton appender;
Logger.getRootLogger().addAppender(appender);
I went through suggestions like
Dynamically add appender with slf4j and log4j2
How to add Log4J2 appenders at runtime programmatically?
I change like below
LogManager.getRootLogger().addAppender(appender);
But I am not able to find the replacement of addAppender(appender);
But its actually should be working, that's why issue.
Is there a way to abbreviate logger name in target's layout? I want to shorten long logger names, eg. instead of com.logback.Foobar I would like to have c.l.Fobar.
This is very often in Java world. For example logback has such abbreviator.
This is not built in NLog, however you could easily add it.
For example:
//Register ${abbr-loggername}
LayoutRenderer.Register("abbr-loggername", (logEvent) => todo.Abbr(logEvent.LoggerName));
See https://github.com/nlog/nlog/wiki/How-to-write-a-custom-layout-renderer
You need for this the LoggerName property in the LogEventInfo object (see API docs)
I use logger org.apache.commons.logging.Log.
File log4j.properties is:
log4j.rootCategory=INFO, S
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.logger.org.springsource.sawt=DEBUG
log4j.logger.org.w3c.tidy=INFO
...................
When I use debug method of org.apache.commons.logging.Log it doesn't log anything.
How to change log4j.properties to enable logger log debug messages?
It is really simple.
If you want to log all debug,info,warn.error,fatal messages from logger you need to take name of logger provided at instantiation by method LogFactory.getLog(loggerName) and append it to log4j.logger`. Thus you get
log4j.logger.loggerName=DEBUG
But it's common that loggerName is String from getClass() method. Thus you can use package name to set logger's level for that package.
I have an API that uses log4j for logging. When I have used the API in my project, though log statements related to project printed with ontl method name, but log statements coming from API is printed full package name format.
In log4j.properties file I am using "%c" (lowercase).
How I can force all project log statements get printed only method name.
Lets say;
I have two classes Main.java and AlarmCategoryImpl.java
AlarmCategroryImpl.java is located on API class, Main.java is defined in my project class.
static Logger logger = Logger.getLogger(AlarmCategoryImpl.class);
static Logger logger = Logger.getLogger(Main.class);
and its log4j output.
2012-12-01/18:13:22.220/EET [INFO][Main->main] starting...
2012-12-01/18:13:22.447/EET [INFO][com.monitor.base.alarmmanagement.alarmconfigurationImpl.AlarmCategoryImpl->copyStructureRecursive] Copying AlarmCategoryImpl
%c means "category name", which is synonymous to "logger name". That means that %c will be expanded to the logger's name.
The logger name is not necessarily the fully-qualified class name. The logger name is the string that is passed in to Logger.getLogger(). Therefore, if you have a class named x.y.z.MyClass, and it has this:
private static final Logger logger = Logger.getLogger("hello");
Then log statements will be generated with hello expanded instead of %c.
That means that the classes in your API are using getLogger(), passing the class name as a parameter. That causes %c to be expanded to the fully-qualified class name when the logs print.
I'm guessing that your non-API classes (in other words, your own project's classes) don't pass-in any value to Logger.getLogger(), or perhaps they use the root logger. To be sure, paste here the line of your code that retrieves the Logger instance.
EDIT as per comment:
Well, is it possible that your Main class is inside the default package? (that is, it is not associated with any package)? If yes, then I don't see any problem.
[INFO][Main->main]: INFO is the level, Main is the class, main is the method.
[INFO][com.monitor.base.alarmmanagement.alarmconfigurationImpl.AlarmCategoryImpl->copyStructureRecursive]: INFO is the level, com.monitor.base.alarmmanagement.alarmconfigurationImpl.AlarmCategoryImpl is the class, copyStructureRecursive is the method.
Our middleware team assignes logger names to each application and that is how they know where to direct our socket appenders to.
I would like to use the standard Logger.getLogger(Clazz.class) paradigm but that does not work with the above constraint. Also we can't log library statements out to our socket appender which would come in handy a lot.
Is there a fairly painless way to map everything from all loggers to this middleware assigned logger?
I think our middleware group messed up in how the configured the enterprise logging system. It looks like there is a setApplication property on the SocketAppender that should be used instead. Regardless, this is what we have to deal with...
You'd like to redirect your "regular" loggers' output to the "middleware logger" directly, i.e. without setting the middleware logger's appender on all the "regular" loggers, right?
If this is the case, try writing your own appender:
class MiddlewareRedirectingAppender extends AppenderSkeleton {
private Logger middlewareLogger = Logger.getLogger("your 'middleware' logger name");
public void doAppend(LoggingEvent event) {
// implement whatever filtering, etc. you want
middlewareLogger.log(...);
}
}
Attach this appender to your "regular" loggers, or just to the root logger (depending on how your "middleware" logger behaves).
Disclaimer: this is just a loose idea, I haven't tested it.