Migrating from log4j-1.2 to slf4j with log4j-2.6 binding - java

I am trying to migrate an existing project from direct log4j to slf4j with log4j binding.
Also I am upgrading the the version of log4j from 1.2 to 2.6
Some of the common code change are :-
1.
import org.apache.log4j.Logger;
.
.
.
private final static Logger log = Logger.getLogger(SearchXYZ.class);
becomes
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
.
.
.
private final static Logger log = LoggerFactory.getLogger(SearchXYZ.class);
2.
import org.apache.log4j.Logger;
.
.
.
private static final Logger logger = Logger.getLogger(XYZ.class);
.
.
.
logger.fatal("FAILURE", throwableObject);
becomes
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
.
.
.
private static final Logger logger = LoggerFactory.getLogger(XYZ.class);
private static Marker fatal = MarkerFactory.getMarker("FATAL");
.
.
.
logger.error(fatal, "FAILURE", throwableObject);
Removed Appenders.
and so on.
One place I'm stuck is Configurator file.
AppConfigLog4jConfigurator.configureForBootstrap();
It gives compile time error saying :-
class file for org.apache.log4j.spi.Configurator not found
What does this function do? What is a possible replacement for this?

First, I am not really sure why you are switching to the SLF4J API since the Log4j 2 API supports everything SLF4J does and much more. In my own code I have found that switching only requires changing the imports and LoggerFactory to LogManager.
Configurator is a class in Log4j 1 that is used to configure Log4j. It is similar to the Configurator class in Log4j 2. You probably want to call one of the initialize methods.

Related

Cannot resolve symbol 'logger' on DuplicateKeyException

In short: the object "logger" is not recognized unlike in many tutorials.
The problem on its own is not very serious and I can easily go around it. However it is very frustrating to see this "logger" stays in red in my intellIj editor. I am going through docs and blogs and I don't see what the problem is.
My snippet:
#Override
public void insertTicketStatut(TicketStatut pTicketStatut) {
String vSQL = "INSERT INTO statut {id, libelle} VALUES {:id, :libelle}";
BeanPropertySqlParameterSource vParams = new BeanPropertySqlParameterSource(pTicketStatut);
NamedParameterJdbcTemplate vJdbcTemplate = new NamedParameterJdbcTemplate(getDataSource());
try {
vJdbcTemplate.update(vSQL, vParams);
} catch (DuplicateKeyException pE){
logger.error("Le TicketStatut existe déjà ! id="+ pTicketStatut.getId(),pE);
}
}
Hovering over logger shows "Cannot resolve symbol 'logger'
Thanks for your help.
Recommend using sl4j
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Then instantiate:
//generic way to declare logger to be able to copy/paste to other classes
//without changing the class name
private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
In dependency, include the binding for sl4j implementation ( can be log4j ).
Can refer here:
logging-with-slf4j
The logger needs to be either instantiated or better injected. It seems that you did not do that. When you use some kind of logging framework like log4j the initialisation would look like this:
static Logger logger = Logger.getLogger(MainApp.class.getName());
When you show us the whole class we can see more details and can guess better.
An tutorial for log4j and spring can be found here.
Indeed, I didn't managed properly my imports:
I added:
private final Log logger = LogFactory.getLog(TicketDaoImpl.class);
from
org.apache.commons.logging.Log;
Stupid lack of attention.
I am having a look also at the doc from log4j/ sl4j on Baeldung. Thanks ^^

Disable automatic import of apache LogFactory and Log

Using eclim whenever I type log. the following packages are automatically imported:
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
The following line is also added the start of my class:
private static final Log log =
LogFactory.getLog(ClassName.class);
Not sure if this behavior is coming from Eclipse or Eclim. Is it possible to disable this?
This is part of vim configuration, I suggest you to read this to solve your issue : http://eclim.org/vim/java/logging.html
In short, you may add
let g:EclimLoggingDisabled = 1
through :VimSettings or to your .vimrc.

Logger working in one file but in another

I have my package structures defined as follows for a dependency named MyProjectwhich is used in another maven project:
In the application.properties file, I have the level for loggers on classes inside the root package defined as follows:
logging.level.abc.xyz.registry=DEBUG
env=DEV
email_to=myname#example.com
I have some logger.info("Test")defined in the following file and I can see them printing on the console.
Myproject/src/main/java/abc/xyz/registry/orm/dao/impl/File1DaoImpl.java
I have some logger.info("Test") and logger.debug("Test") defined in the following file as well. However,I cannot see the logger printing any information on the console :
Myproject/src/main/java/abc/xyz/registry/orm/dao/impl/File2DaoImpl.java
Any reason why this is happening?
Logger Declarations in both the classes :
private static final Logger logger = LoggerFactory.getLogger(File1DaoImpl.class);
private static final Logger logger = LoggerFactory.getLogger(File2DaoImpl.class);
File1DaoImpl has logger defined in the following manner(Basically I am not printing any variables here and just messages)
logger.info("Starting Insert method in Eclipse May 22 File1DaoImpl.insert() .....");
I have it defined like the following in File2DaoImpl which doesn't works:
logger.debug("Records Updated in File2DaoImpl params : " +
"employeeId:"+employeeId+
",tokenID:"+tokenID+
",newEmpID:"+newEmpID+
",userComments="+userComments
);
logger.info("INSERTED Records from the File2DaoImpl:"+
"employeeId:"+employeeId+
",tokenID:"+tokenID+
",newEmpID:"+newEmpID+
",userComments="+userComments
);
I have following import statements in both the classes:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

How to add "import" statements from templates in IntelliJ?

I have defined the following live template in IntelliJ:
private static final Logger log = LoggerFactory.getLogger($CLASS_NAME$.class);
I use it to insert logger variable to a class.
Is it possible to define so that template also adds
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
to the file if these definitions are still absent?
Define it fully in the Live template:
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger($CLASS_NAME$.class);
and IntelliJ should auto reformat the expression to an import. (Assuming you already have the lib JAR downloaded and configured with IntelliJ).
Edit: As comment says: the Shorten FQ Names check-box should be checked (which it is by default)
Tested with IntelliJ IDEA 15.0.4
Now its possible to add live templates with static imports:
You have to check static import in Options
#org.junit.Test
public void should$EXPR$when$CONDITION$() {
org.junit.Assert.assertThat(null, org.hamcrest.CoreMatchers.is(org.hamcrest.CoreMatchers.nullValue()));
}

Using java.util.logging.Logger in a jar file that will be displayed in LogCat in Android

Android Studio 0.8.1
java version "1.7.0_60"
Hello,
I have created a jar file and calling this from my Android App. For testing purposed I want to put some logging in the jar file that will be displayed in the LogCat window.
I have decided to use Java's java.util.logging.Logger class. However, when I run my app with No Filter I cannot see any of my log messages being displayed.
import java.util.logging.ConsoleHandler;
import java.util.logging.SimpleFormatter;
import java.util.logging.Handler;
import java.util.logging.Logger;
import java.util.logging.Level;
public class RequestFactory extends WebServiceRequestFactory {
private static final Logger log = Logger.getLogger("RequestFactory");
public RequestFactory() {
ConsoleHandler consoleHandler = new ConsoleHandler();
log.addHandler(consoleHandler);
consoleHandler.setFormatter(new SimpleFormatter());
log.log(Level.FINE, "LOG: Initialized overloaded constructor");
System.out.println("Initialized overloaded constructor");
}
.....
I have set the above to be displayed in the Console. However, the System.out.println always prints out. However, I don't want to use the System.out.println for displaying logs messages.
If possible I would like to stick to java's logging class.
In the LogCat window I can see the System.out.println(...), but not the log.log(...) one:
I/System.out﹕ Initialized overloaded constructor
Am I doing something wrong here?
Many thanks for any suggestions
The default level for the ConsoleHandler is INFO. The default level for the logger is inherited from its parent. By default, the root logger is usually set to INFO. A FINE log message will not be reported using the default settings. Change the log level of the ConsoleHandler to ALL and change the level of the logger to FINE.
log.setLevel(Level.FINE);
consoleHandler.setLevel(Level.ALL);

Categories

Resources