The method getRootLogger() is undefined for the type Logger migration from Log4j 1.x to 2.x - log4j

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.

Related

How to limit logging for an external package when converting from log4j to log4j2... What is the correct properties file setting?

In log4j I have: "log4j.logger.org.apache.struts2=ERROR"
What is the equivalent properties file setting in log4j2?
The configuration of LoggerConfig elements in log4j2.properties uses the syntax:
logger.<id>.<property_name>=<property_value>
where <id> is any string identifier (used only to group the configuration of the same Logger, otherwise ignored), <property_name> is the Java bean property you want to set (cf. LoggerConfig javadoc and its setter methods).
Hence the equivalent Log4j 2.x configuration for the one in your question is:
logger.1.name=org.apache.struts2
logger.1.level=ERROR

How Can I Add Console Appender In Logger Object Using log4j-over-slf4j Dependency?

I can't be able to add console appender using logger.addAppender method with log4j-over-slf4j 1.7.x dependency. Moreover, I am unable to set target of that particular Console Appender(i.e., SYSTEM_OUT/SYSTEM_ERR).
I have initialized a console appender object and tried to push that reference into addAppender method by typecasting that reference to Appender. But in that case I am unable to set Target/WriterLocation(i.e., SYSTEM_OUT/SYSTEM_ERR) for console appender reference. I have used the below code snippet-
ConsoleAppender ca = new ConsoleAppender();
ca.setWriter(new OutputStreamWriter(System.out)); // this line is not compatible with log4j-over-slf4j jar
ca.setLayout(new PatternLayout("%-5p [%t]: %m%n"));
logger.addAppender(ca);
Please help me to sort out this problem.
What you are doing doesn't make a lot of sense. log4j-over-slf4j will route calls like logger.debug(), logger.info(), etc to slf4j and then presumably to some other logging framework to be logged. Your code is trying to manipulate log4j 1 objects which won't be involved in logging since you are routing log events to SLF4J (which is why many of them aren't supported by log4j-over-slf4j).
In order to help you we would know what logging implementation you really want to use.

Can I check this in ArchUnit

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.

Configure CXF JAX-WS service to work with MOXY

Although I've added a jaxb.properties with MOXY factory and I see that the JAXB was switched to moxy, CXF has a method named createRIContext in the JAXBUtils class which loads hard coded the sun JAXB implementation.
Is there a way to override it and use moxy instead?
The problematic code is the following:
// fall back if we're using another jaxb implementation
try {
riContext = JAXBUtils.createRIContext(contextClasses
.toArray(new Class[contextClasses.size()]), tns);
}
It loads hard coded the "com.sun.xml.bind.v2.ContextFactory" class and use it to create a JAXB context.
The 3.0.0-milestone2 version of CXF should handle Moxy quite a bit better. That said, there are still bugs in Moxy that have prevented all of the CXF unit and system tests to pass with it so we don't have the same level of confidence with Moxy as we do with the JAXB RI.
(any help with testing 3.0.0 would be greatly appreciated)

The type or namespace name 'ThreadContext' does not exist in the namespace 'log4net'

I am trying to use the following line of code to pass a property to a log4net appender:
log4net.ThreadContext.Properties["LogName"] = processID.ToString();
but, when I compile I keep getting the error The type or namespace name 'ThreadContext' does not exist in the namespace 'log4net'
I am "using" log4net and I am including the Sitecore.Logging DLL in the references of the project. Log4net is being used by this project and both loggers and appenders work just fine. The only thing is that when I type log4net in code and type the ".", intellisense doesn't diaply any reference to either GlobalContent or ThreadContent.
Seems that Sitecore uses an older version of log4net in their Sitecore.Logging because there is no ThreadContext class present there, or they removed it from their implementation.
They use version 1.2.0 as far as i can see.

Resources