JavaRush/Java Blog/Random EN/We connect logging to our JavaRush project (slf4f and log...

We connect logging to our JavaRush project (slf4f and log4j)

Published in the Random EN group
members
You can read how to connect Maven to your JavaRush project in this article of mine . The following assumes that you know how to do this. If we want to use slf4j, then we need a dependency:
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.30</version>
</dependency>
After this we do:
public static void main(String[] args) throws IOException {
    Logger logger = LoggerFactory.getLogger(Solution.class);
    logger.info("hello world!");
}
And we get the error:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Which is generally logical. slf4j is a wrapper, but what did we wrap it around? Nothing? Well, we got an error. You can wrap it around one of the following: log4j, util.logging, NOP, System.err, JCL, logback. More details in the original instructions . Let's look at the example of log4j and System.err: 1) Let's start with something simple, with System.err . To do this, add this one (slf4j-simple) to the previous dependency (slf4j-api).
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.7.30</version>
</dependency>
We take the same versions, as of May 10, 2021 it is 1.7.30. If you take dependencies from https://mvnrepository.com/ , do not forget to remove
<scope>test</scope>
Otherwise, your logs will only work in tests. 2) Now let's try to configure log4j , for this we need to add 2 dependencies: log4j itself and a layer between slf4j and log4j (slf4j-log4j12). It looks like this:
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.30</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
It’s clear that we need to remove (slf4j-simple) so that slf4j doesn’t get confused about which logger it should work with. And leave (slf4j-api), since this is slf4j. Regarding the 2 added dependencies. log4j we are adding the old one (from May 2012, I don’t know why, you can experiment with others). Its version is 1.2.17. In order for slf4j to work with it, you need an appropriate layer - slf4j-log4j12, where the last characters (12) mean the log4j version. In this case, the version of slf4j-api and slf4j-log4j12 should be 1.7.30 - the latest stable one at the moment. After you have connected the dependencies and updated the Maven project. For log4j to work, you need 2 more things: 1) Set up a configuration file. We read, select, copy. 2) Read the log4j configuration before work:
PropertyConfigurator.configure("d:\\Java\\JavaRushTasks\\4.JavaCollections\\log4j.properties");
Well, or where you have the file. If you want to see all the logs, do not forget to set the logging level to ALL. We add to the resume - log4j & slf4j. You are gorgeous! If it helped - like, subscribe, bell!)))
Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet