My entire system is logged by slf4j with log4j implementation.
I had a problem when a new module used a logback-classic depdency for logging which cannot be excluded from pom.xml file since it breaks it.
First I tried to look for solution in this 3rd side depdency but couldn't find any solutions so I thought maybe a bridge between them could be something that solves it.
What I mainly looking for is to split my logs between the deafult console logging of the dependency to my own log4j.xml loggers and appenders so I can use the separetly..
Is there any bridge so I could use both logback-classic and log4j under slf4j with example?
Thanks!
You said:
I had a problem when a new module used a logback-classic depdency for logging which cannot be excluded from pom.xml file since it breaks it.
Is this an assumption or did you try it? Also, if you did try it did you remove the logback-core dependency as well? The only way removing these dependencies would break the module is if the module depends on the logging implementation since logback natively implements the slf4j API. If the module does depend on the implementation rather than the API/interface I don't think there's anything you can do without either removing those dependencies (changing the module source code) or writing some stubbed versions of the implementation classes that the module depends on.
When I write code that follows the pattern in the logback manual I'm able to swap the implementation from logback to log4j2 without any issues as long as I don't introduce dependencies on the logback implementation classes.
Here is the example I wrote:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Main {
private Logger log = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) {
Main main = new Main();
main.main();
}
public void main(){
log.trace("trace msg");
log.debug("debug msg");
log.info("info msg");
log.warn("warn msg");
log.error("Error msg");
log.info(log.getClass().getName());
}
}
Here are the dependencies in the pom:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.22</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
Here is the output:
00:48:26.378 [main] DEBUG blah.Main - debug msg
00:48:26.380 [main] INFO blah.Main - info msg
00:48:26.380 [main] WARN blah.Main - warn msg
00:48:26.380 [main] ERROR blah.Main - Error msg
00:48:26.380 [main] INFO blah.Main - ch.qos.logback.classic.Logger
Now I change the pom to replace the logback jars with log4j2:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.22</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
</dependency>
Here's the output after making this change:
2017-09-03 00:52:21,630 INFO b.Main [main] info msg
2017-09-03 00:52:21,631 WARN b.Main [main] warn msg
2017-09-03 00:52:21,631 ERROR b.Main [main] Error msg
2017-09-03 00:52:21,632 INFO b.Main [main] org.apache.logging.slf4j.Log4jLogger
So based on this I think you should, if things are implemented the "right way", be able to swap the logback jars with log4j2 and it should "just work".
You also said:
What I mainly looking for is to split my logs between the deafult console logging of the dependency to my own log4j.xml loggers and appenders so I can use the separetly..
Now it's entirely clear to me what you were asking but I think you wanted to have log messages from the module go to console as well as any logs you are using with your log4j2 configuration. If this is the case that's as simple as modifying your log4j2 configuration - add a logger with the appropriate name and assign the appropriate appenders. For example if your module's classes are com.my.package.Class1, com.my.package.Class2, com.my.package.Class3, etc then you could create a logger for com.my.package and give it a console appender along with the appropriate file appenders.
Hope this helps!
Related
Running into an error while initializing context for an azure blob container storage..
Would greatly appreciate any help
>org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class [listeners.AzureBackupManagerContextListener]
java.lang.NoClassDefFoundError:
[....]
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.AnnotationIntrospector$XmlExtensions
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1412)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1220)
... 76 more
I also hit this issue and was able to solve this with adding fasterxml jackson dependencies:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.13.3</version>
</dependency>
I'm getting the following error. It seems there are multiple logging frameworks bound to sl4j. Not sure how to resolve this. Any help is greatly appreciated.
14:42:35,411 ERROR [stderr] (MSC service thread 1-3) SLF4J: Class path contains multiple SLF4J bindings.
14:42:35,412 ERROR [stderr] (MSC service thread 1-3) SLF4J: Found binding in [vfs:/content/offer-warehouse-processor-api.war/WEB-INF/lib/log4j-slf4j-impl-2.7.jar/org/slf4j/impl/StaticLoggerBinder.class]
14:42:35,412 ERROR [stderr] (MSC service thread 1-3) SLF4J: Found binding in [vfs:/content/offer-warehouse-processor-api.war/WEB-INF/lib/slf4j-log4j12-1.7.21.jar/org/slf4j/impl/StaticLoggerBinder.class]
The message tells you that you have both slf4j-log4j12-1.7.21.jar and log4j-slf4j-impl-2.7.jar on your classpath. slf4j-log4j12 routes all SLF4J logging to log4j 1.2. Log4j-sfl4j-impl routes all logging to log4j 2. You need to remove the one you don't want. For example, if you want to use log4j 2 then remove slf4j-log4j12-1.7.21.jar from your project. If you aren't sure how it got included and your are using Maven then run
mvn dependency:tree >mvn.txt
and then look in the mvn.txt file that was created and find where the jar is being included and what dependency it is under from your pom.xml. Then add an exclusion like
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
in the dependency that is including it.
I am using slf4j in my application for logging purpose. But it's not creating log file in file system but it's logging to console. Below is my log4j.properties file:
# Root logger option
log4j.rootLogger=DEBUG, RollingAppender,stdout
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Redirect log messages to a log file
log4j.appender.RollingAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RollingAppender.File=E:\\SalesTerritory.log
log4j.appender.RollingAppender.DatePattern='.'yyyy-MM-dd
log4j.appender.RollingAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.RollingAppender.layout.ConversionPattern=[%p] %d %c %M - %m%n
And dependency I am using in pom.xml for logging is:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>[enter image description here][1]
Please find the dependency jar list in attached image files.
[1]: https://i.stack.imgur.
com/KGa2x.png
use this below Dependency for log4j
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
Use INFO in log4j.rootLogger instead of DEBUG because INFO designates informational messages that highlight the progress of the crawl at a
coarse-grained level.
Im trying to add the modelgen jar to my maven project to enable automatic Metamodel class generation however I am getting the following error when I try to compile the project:
Failed to execute goal org.bsc.maven:maven-processor-plugin:2.2.4:process (process) on project Project-per: Error executing: java.lang.RuntimeException: java.lang.SecurityException: class "org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProperties"'s signer information does not match signer information of other classes in the same package
As best as I can determine there is most likely a miss match in the JAR signatures causing this. However I don't understand why or how to fix the problem.
My POM (Relevant parts only)
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.0-RC2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.nosql</artifactId>
<version>2.5.0-RC2</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa.modelgen</artifactId>
<version>2.4.2</version>
</dependency>
NB. Everything works fine with the nosql and the eclipselink, its just when I add the modelgen.
OK, as best I can tell I was right, the problem was caused by a signature difference in the JAR files. How this can happen on a library as popular as eclipselink is beyond me but hey.
My solution was to use a snapshot as they are generally not signed. Given that I was unable to obtain snapshot from central I used the following repo to obtain it;
<repository>
<id>sonatype-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</repository>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa.modelgen</artifactId>
<version>2.6.0-SNAPSHOT</version>
</dependency>
Add following dependency into pom
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<versionRange>[2.2.4,)</versionRange>
<goals>
<goal>process</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore />
</action>
</pluginExecution>
I'm trying to push a message from server to client that way :
PushContext pushContext = PushContextFactory.getDefault().getPushContext();
pushContext.push("/registrationEvent", "There was another registration");
My problem is that i've the following error
ava.lang.NoClassDefFoundError: Could not initialize class org.primefaces.push.PushContextFactory
But I think that this is due to a issue at the initialization of the projet :
java.lang.NoClassDefFoundError: org/atmosphere/cpr/AsyncSupportListenerAdapter
I've try to add jar atmosphere file... Without success. Have I done something wrong ? I'm using glassfish 3.1.
Thanks !
Primefaces MigrationGuide inform: "PrimeFaces Push is reimplemented, PushContext is deprecated, use EventBus instead along with the new Push API."
In this case, on pom.xml, put the 2.2.1 atmosphere version. Probably you are using a old atmosphere version. If you trying to use the Primefaces 5.0, put the code below:
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>5.0</version>
</dependency>
<dependency>
<groupId>org.primefaces.extensions</groupId>
<artifactId>primefaces-extensions</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.atmosphere</groupId>
<artifactId>atmosphere-runtime</artifactId>
<version>2.2.1</version>
</dependency>