Am processing files using java , there are 2 or 3 Java components to process files.My requirement is for file must have a log file and its processing details will be logged in relevant log file.
Problem is for single file it works well but with multiple file am getting problem.
When multiple files are getting processed logger is logging log detail of file1.txt logs to file2.log instead of file1.log...
public Class FileProcessComponent1
{
public void process(String fileName)
{
Logger Log = Logg.getLogger1(fileName,this.getClass());
log.info("file1 logging");
}
}
public Class FileProcessComponent2
{
public void process(String fileName)
{
Logger Log = Logg.getLogger1(fileName,this.getClass());
log.info("file1 logging");
}
}
public Class Logg
{
public static Logger getLogger1(String fileName,Class clazz) throws Exception
{
if(fileName == null || "".equals(fileName.trim()))
throw new Exception("File Name or Map for File Name is Null");
fileName = "/home/logs/"+fileName+".log";
Logger logger = Logger.getLogger(clazz.getCanonicalName()+":"+System.nanoTime());
logger.setAdditivity(false);
FileAppender appender = new DailyRollingFileAppender(new PatternLayout("%d{ISO8601}\t%p\t%c\t%m%n"), fileName, "'.'yyyy-MM-dd");
logger.addAppender(appender);
logger.setLevel(Level.DEBUG);
return logger;
}
}
I think you want to create a logger pointing to a unqiue file for each files comes for processing.
Try this,
a) At the point where you start processing a new file, Create a new Logger with file name as Loggername.
b) Create the Appender with filename.log and assign the appender to the logger.
Now, when you try to get the logger, always use the filename in getLogger().
Once you are done with processing a file, remove the logger/appender. (this is very important)
Related
I'm actually adding java logging (can't use other framework) to my project. I build my app on a .war, and deployed it over Weblogic, the logger is working with my logging.properties config, except for the formatter i don't know why the app is ignoring it.
This is my class where i prepare the logger;
public class CtgLogger {
private static final String LOAD_ERROR = "Properties could not be loaded.";
private static final Map<String, Level> LEVEL_MAP;
private static final Logger LOGGER = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
static {
final InputStream inputStream = CtgLogger.class.getResourceAsStream("/logging.properties");
try {
LogManager.getLogManager().readConfiguration(inputStream);
} catch (Exception e) {
Logger.getAnonymousLogger().severe(LOAD_ERROR);
Logger.getAnonymousLogger().severe(e.getMessage());
}
// and I add the LEVEL_MAP to the logger...
And this is my properties...
handlers = java.util.logging.FileHandler
java.util.logging.FileHandler.pattern=logsfolder/CTGLOG_%g.log
java.util.logging.FileHandler.level=ALL
java.util.logging.FileHandler.limit=3000
java.util.logging.FileHandler.count=6
#java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
#If I use the SimpleFormatter, apps goes well with it format.
java.util.logging.FileHandler.formatter = com.package.my.log.JsonCustomFormatter
#If I use my custom formatter, the weblogic works with a XMLFormatter (default)
I know the .properties is working, because logger is working with the pattern, limit and count I setted.
PD: If i run my app with JUnit, logs are working with my custom formatter, but do not at weblogic! Don't know why!
Weblogic is going to have multiple class loaders. The standard LogManager can only see classes loaded via the system class loader. Check the Server Log for errors related to not finding your custom class. If that is the case, you have to move your formatter to the system classloader. Otherwise you have have to use code to install your formatter from your web app which is running in a child classloader.
There are also bugs in the LogManager.readConfiguration and alternative methods to use in JDK9 and later.
Using Eclipse and java standard logger may be painful. I found something to produce similar output to Log4J:
"%d{HH:mm:ss,SSS} %-5p %m (%F:%L) in %t%n" in Log4J : you can click on reference and you are there log was issued
21:36:37,9 INFO process model event Digpro2021a/digpro.Digpro(Digpro.java:358) in processModelEvent
21:36:37,9 INFO start polling Digpro2021a/digpro.Digpro(Digpro.java:398) in processEventAutoreload
21:36:37,9 INFO reload now Digpro2021a/digpro.Digpro(Digpro.java:370) in processModelEvent
public class Digpro {
protected static final Logger L = Logger.getLogger("Digpro");
//logger conf
static {
L.setLevel(Level.FINE);
Handler handler = Logger.getLogger("").getHandlers()[0];
handler.setLevel(Level.FINE); // Default console handler
handler.setFormatter(new Formatter() {
#Override
public String format(LogRecord r) {
Date d = new Date(r.getMillis());
String srcClassLong = r.getSourceClassName();
String[] aClass = srcClassLong.split("\\$")[0].split("\\.");
String srcClass = aClass[aClass.length - 1];
StackTraceElement elem = (new Throwable()).getStackTrace()[7];
int line = elem.getLineNumber();
String modulName = elem.getModuleName();
return String.format("%tH:%tM:%tS,%tl %.7s %s %s/%s(%s.java:%d) in %s\n", d, d, d, d, //
r.getLevel(), r.getMessage(), // LEVEL and message
modulName, srcClassLong, srcClass, line, r.getSourceMethodName()); //ref to click on
}
});
}
...
public static class TestDigpro extends Digpro {
//TESTING:
#Test
public void testLogFormat() {
L.info("poll info");
L.fine("got fine");
}
}
}
produses:
21:51:20,9 INFO poll info Digpro2021a/digpro.Digpro$TestDigpro(Digpro.java:723) in testLogFormat
21:51:20,9 FINE got fine Digpro2021a/digpro.Digpro$TestDigpro(Digpro.java:724) in testLogFormat
Although I've read multiple other questions and answers regarding this issue, I didn't find the answer and would appreciate any help.
public static void rerouteAppenderToFile(File file, Logger... log) {
for (Logger logger : log) {
rerouteAppenderToFile(file, logger);
}
}
public static void rerouteAppenderToFile(File file, Logger log) {
log.setAdditivity(false);
log.setLevel(Level.DEBUG);
RollingFileAppender fa = new RollingFileAppender();
fa.setName("RollingFileAppender_" + log.getName());
fa.setMaxBackupIndex(3);
fa.setMaxFileSize("20MB");
File folder = file.getParentFile();
folder.mkdirs();
if (folder.exists() && folder.isDirectory()) {
fa.setFile(file.getAbsolutePath());
fa.setLayout(new PatternLayout(LOGGER_PATTERN));
fa.setThreshold(Level.DEBUG);
fa.setAppend(true);
fa.activateOptions();
log.addAppender(fa);
}
}
What I'm trying to achieve here is being able to append multiple loggers (2 or more) to the same file using a RollingFileAppender. For some reason the files aren't rolled and the logs keep getting larger
The problem is that you create multiple RollingFileAppenders that write to the same file.
So even if one RollingFileAppender tries to roll over to a new file it cannot do so, since the file is locked by the other RollingFileAppenders.
What you could do is to create only one RollingFileAppender instance and reroute the loggers to this single instance.
The following code gives the idea, but it won't work if you call rerouteAppenderToFile() several times for a single file.
public static void rerouteAppenderToFile(File file, Logger... log) {
RollingFileAppender fa = createAppender(file);
for (Logger logger : log) {
rerouteAppenderToFile(f, logger);
}
}
public static Appender createAppender(File file) {
RollingFileAppender fa = new RollingFileAppender();
fa.setName("RollingFileAppender_" + log.getName());
fa.setMaxBackupIndex(3);
fa.setMaxFileSize("20MB");
File folder = file.getParentFile();
folder.mkdirs();
fa.setFile(file.getAbsolutePath());
fa.setLayout(new PatternLayout(LOGGER_PATTERN));
fa.setThreshold(Level.DEBUG);
fa.setAppend(true);
fa.activateOptions();
return fa;
}
public static void rerouteAppenderToFile(Appender fa, Logger log) {
log.setAdditivity(false);
log.setLevel(Level.DEBUG);
log.addAppender(fa);
}
I have successfully created a log at each process run time. The issue I am having now is any packages that are called that are not children of the current running process does not write its log to my file. For example I create a new log file called running-.log. The process that is running is com.me.foo inside of this class there is a call to a method in com.you and another one in com.zee . I would like to have com.you and com.zee logs write to the running-.log and not to the console log. It isn't as simple as just changing the getLogger() method to be a child of com.me.foo. Some of the logs are written out from third party jars. I am at a loss. If you need to see more code or some additional info, please let me know. There has to be another way to handle this.
Thanks
Code to create the log file dynamically
public void createLogInstance(String packaging,String appenderName, String logFileName){
Logger logger = Logger.getLogger(packaging);
Appender fileAppender = logger.getAppender(appenderName);
if(fileAppender != null){
logger.removeAppender(fileAppender);
}
//Create the root appender
ConsoleAppender console = new ConsoleAppender();
String pattern = ....;
console.setLayout(new PatternLayout(pattern));
console.setThreshold(Level.FATAL);
console.activateOptions();
logger.addAppender(console);
FileAppender fa = new FileAppender();
fa.setName(appenderName);
fa.setFile(logFileName);
fa.setLayout(new PatternLayout(..));
fa.setThreshold(Level.DEBUG);
fa.setAppend(true);
fa.activateOptions();
logger.setAdditivity(false);
logger.addAppender(fa);
}
com.zee log
private static Logger logger = LoggerFactory.getLogger(Zee.class);
com.you log
private static Logger logger = LoggerFactory.getLogger(You.class);
I was missing the rootLogger. Changed this line
Logger logger = Logger.getLogger(packaging);
to
Logger logger = Logger.getRootLogger();
If someone has a better way please let me know.
In my application I read in an inputfile (e.g. myFile1.txt) and create an outputfile with the same name (e.g. myFile2log). Point is that the inputfile will be read within the java application and not given as command line parameter. Therefore it is required to set an appender in the application, e.g.
public class Example {
private static final Logger LOG = Logger.getLogger(Example.class);
public Example() throws IOException {
FileAppender appender = new DailyRollingFileAppender(new PatternLayout(
PatternLayout.DEFAULT_CONVERSION_PATTERN), "yourfilename.log",
"'.'yyyy-MM-dd");
LOG.addAppender(appender);
LOG.setLevel((Level) Level.DEBUG);
LOG.debug("blabla");
new RandomClass();
}
public static void main(String[] args) throws IOException {
new Example();
}
}
public class RandomClass {
private static final Logger LOG = Logger.getLogger(RandomClass.class);
public RandomClass() {
LOG.debug("hello Randomclass");
}
}
And here is the problem: if I do the above the custom file appender only works for the specific class where the fileappender was defined, but not on any other class. Therefore the output of the "RandomClass" will not be written into this logfile, but which is what is required. How can I achieve that?
If you would like to set the appender dynamically you should try setting the new appender to the root logger:
Logger logger = Logger.getRootLogger();
FileAppender appender = new DailyRollingFileAppender(new PatternLayout(
PatternLayout.DEFAULT_CONVERSION_PATTERN), "yourfilename.log", "'.'yyyy-MM-dd");
logger.addAppender(appender)
You can use log4j.properties file in your class path then simply you can initilize logger in all the classes.
private static final Logger LOG = Logger.getLogger(Example.class);
and Appender all not required in constructor. your property file should contain
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=yourfilename.log
log4j.appender.R.MaxFileSize=2048KB
I have one query is that if I write a simple Java class and I want to see the logs that are executed at the behind, such that are executed at the backend by the JVM. I have log4j JAR with me. Could you please give me advice to how to achieve this?
I would to see the same logs that are generated in case of a WebApp, so I can reuse it for a Java App.
try looking at log4j api/demo here http://www.vaannila.com/log4j/log4j-tutorial/log4j-tutorial.html
you can generate log for any class and any application afaik using log4j , which needs not necessarily be a web app
Here is a very simple example that you should be able to adapt to what you are trying to achieve.
Step 1:Import log4j jar file in your project
Step 2:-Create a Log java file
public Log(Class className) {
logger = Logger.getLogger(className.getClass());
logger.getClass();
final String LOG_PROPERTIES_FILE_NAME = "log4j.properties";
props = loadLogFile(LOG_PROPERTIES_FILE_NAME, className);
//props =getPropsFile(LOG_PROPERTIES_FILE_NAME, className);
PropertyConfigurator.configure(props);
}
public static Properties loadLogFile(String propertyFile, Class className) {
//String filePath="";
try {
Properties ps = null;
ResourceBundle bundle = ResourceBundle
.getBundle("resource.DbProperties");
String logPath = bundle.getString("logs.path");
File filePath = new File(logPath);
if (!filePath.exists()) {
filePath.mkdirs();
}
props.clone();
props = getPropsFile(propertyFile, className);
props.setProperty("info_file_path", filePath.toString());
// PropertyConfigurator.configure(props);
} catch (Exception e) {
e.printStackTrace();
}
return props;
}
public void info(String message) {
logger.info(message);
}
public void warn(String message) {
logger.warn(message);
}
public void error(String message) {
logger.error(message);
}
public void fatal(String message) {
logger.fatal(message);
}
public void printStackTrace(Throwable ex) {
StringWriter sw = new StringWriter();
ex.printStackTrace(new PrintWriter(sw));
logger.error("\n" + sw.toString());
}
log4j.properties
log4j.rootCategory=LOGFILE
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=${info_file_path}\\info_Pension_application.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.Threshold=INFO
log4j.appender.LOGFILE.Threshold=DEBUG
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
If your "simple Java class" happens to be in a Java EE container (like a .jsp or servlet in Tomcat, JBoss or WebSphere), then all you need to do is:
1) import org.apache.log4j.Logger;
2) declare a static instance in your class:
EXAMPLE: `static Logger logger = Logger.getLogger ("MyApp");`
3) Use your logger
EXAMPLE: `logger.info ("something interesting happened");`
4) If you include log4j.java and your own log4j.properties in your CLASSPATH, then you don't need to do anything else.
Here's a bit more info on using log4j with Tomcat or app servers:
http://tomcat.apache.org/tomcat-5.5-doc/logging.html
http://logging.apache.org/log4j/1.2/manual.html
http://www.tutorialspoint.com/log4j/log4j_quick_guide.htm
Here's a very simple example program that "does logging", from the above tutorialspoint.com link:
import org.apache.log4j.Logger;
import java.io.*;
import java.sql.SQLException;
import java.util.*;
public class log4jExample{
/* Get actual class name to be printed on */
static Logger log = Logger.getLogger(
log4jExample.class.getName());
public static void main(String[] args)
throws IOException,SQLException{
log.debug("Hello this is an debug message");
log.info("Hello this is an info message");
}
}