Logger working in one file but in another - java

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;

Related

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

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.

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()));
}

Java Logging to Multiple Files

I have an application which keeps track of multiple students. When processing infromation on that student, I want their log messages to go into that log file.
logs/system.log
logs/abby.log
logs/brett.log
logs/catherine.log
The system can add more students dynamically, so I can't specify each student in my log config file. How can I, at runtime, specify that a logger should write information to catherine.log ?
Which Logging Framework are you using? Here is an example if you are using Log4j:
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;
import org.apache.log4j.FileAppender;
public class MyTestClass {
/* Logger Instance should always be kept static final */
private static final Logger LOGGER = Logger.getLogger(MyTestClass.class);
public static void main(String args[]) {
/* Create Dynamic FileAppender */
SimpleLayout myLayout = new SimpleLayout();
FileAppender nwAppender = new FileAppender(myLayout,"file_nm",false);
LOGGER.addAppender(nwAppender);
LOGGER.setLevel((Level) Level.INFO);
/* Write Level : Debug */
LOGGER.debug("*** DEBUG ***");
/* Write Level : Info */
LOGGER.info("*** INFO ***");
/* Write Level : Error */
LOGGER.info("*** ERROR ***");
}
}
How can I, at runtime, specify that a logger should write information to catherine.log ?
There is no logging.properties option to enable this behavior. You have to write code to create a logger (strongly referenced) and installed a FileHandler on that logger.
Messages for abby.log are simultaneously written to the system log and to abby.log.
You should create a logger namespace such that the system file handler is installed on the root logger and each student is a child logger with setUseParentHandlers set to false.
If multiple threads are processing abby, it will open up abby-1, abby-2
That is because you are created multiple FileHandlers with the same file name. Create a Map and remember what you have opened.

Where does my Log4J output go to(ran the HelloWorld Log4J example)?

I'm trying to learn Log4J and have a problem with understanding how to incorporate the logging and view output. This is my code:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class HelloWorldLog4J {
private static final Logger logger = LogManager.getLogger("HelloWorld");
public static void main(String[] args) {
logger.info("Hello, World! ammmm");
}
}
Where does the output go to ? I assume that there's some text file in the log4j install-directory?
This is how my log4J directories looks like
You need to post your log4j.properties file. In many cases , this is how specify the output file in log4j.properties file :-
log4j.appender.outLogger.File=appname-out.log # normal output
log4j.appender.errLogger.File=appname-err.log # Error due to logger.error

How to automatically add code to every class I create

In Eclipse (Java), how do I automatically add code to every class I create. Suppose I create a class called Foo, I want this code to automatically go in the preamble/state:
private final Logger log = LoggerFactory.getLogger(this.getClass());
and the appropriate slf4j import to be automatically imported. Similarly, I would like the constructor to automatically show up. Full example of what I would like to see after I click the create button:
package test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Foo {
private final Logger log = LoggerFactory.getLogger(this.getClass());
public Foo() {
}
}
This should help. You can modify whichever template suits your purpose.
You could change the New Java Files and Class body templates to get what you want.
In the Preferences, under Java-> Code Style -> Code Templates, there is New Java Files where you would add the imports, at the appropriate place.
Change the Class body template like this
private final Logger log = LoggerFactory.getLogger(this.getClass());
public void ${type_name}() {
}
to add the logger and a default, public constructor.
Making those 2 changes will automatically add what you want when you create a new Java class file with Eclipse.

Categories

Resources