473,322 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Issue regarding log4j

44
Hi there,

I am working on server which used thread pool to process each client request by separate dedicated thread.
My intention was each thread ( of thread pool ) have its own logger and hence all client processed by that thread must be logged in corresponding thread specific logger.

My problem is that logger merging log content with other logger content, my doubt is Logger.getLogger(...) function return wrong logger object.

I am creating and configuring logger at run time using following function.

where:
sLoggerName : Current thread name ( Thread.currentThread.getName() )

m_sFilePath : log file path
m_sExt : log file extention
m_layout : static layout object

Expand|Select|Wrap|Line Numbers
  1. public static Logger getLogger( String sLoggerName )throws IOException
  2.  {
  3.         boolean bLoggerExists = false;
  4.         if(LogManager.exists(sLoggerName) != null)
  5.             bLoggerExists = true;
  6.         Logger logger = Logger.getLogger(sLoggerName);
  7.         if( !bLoggerExists )
  8.         {
  9.             //Logger is not exists so create appender for logger
  10.             String sFileName = m_sFilePath + sLoggerName + m_sExt;
  11.             RollingFileAppender appender = new RollingFileAppender(m_layout, sFileName, true );
  12.             appender.setMaxBackupIndex( m_iMaxBufferIndex );
  13.             appender.setMaxFileSize( m_sLogMaxFileSize );
  14.             logger.addAppender( appender );
  15.             logger.setLevel( m_logLevel );
  16.             logger.setAdditivity(false);
  17.         }
  18.  
  19.         return logger;
  20.     } 
  21.  
  22.  
for example logger for thread 5 wirtes log in file for thread 8 and so on .....

Is there any thing I am missing in code ????

note: I have used java's executor server for thread pooling

log4j.property ( for root logger )

Expand|Select|Wrap|Line Numbers
  1. # Category Configuration
  2. log4j.rootLogger=INFO,Konsole,Roll
  3. # Console Appender Configuration
  4. log4j.appender.Konsole=org.apache.log4j.ConsoleAppender
  5. log4j.appender.Konsole.layout=org.apache.log4j.PatternLayout
  6. # Date Format based on ISO­8601 : %d
  7. log4j.appender.Konsole.layout.ConversionPattern=%d [%t] %5p %c ­ %m%n
  8. # Roll Appender Configuration
  9. log4j.appender.Roll=org.apache.log4j.RollingFileAppender
  10. log4j.appender.Roll.File=D\:/4YsClient/Log/ErrorFileList.log
  11. log4j.appender.Roll.MaxFileSize=100MB
  12. log4j.appender.Roll.MaxBackupIndex=10
  13. log4j.appender.Roll.layout=org.apache.log4j.PatternLayout
  14. # Date Format based on ISO­8601 : %d
  15. log4j.appender.Roll.layout.ConversionPattern=%d [%t] %p (%F:%L) ­ %m%n
  16.  

pleas suggest, your help make me happy,

Thank you,
Abhinay
Jan 31 '09 #1
5 4085
Abhinay
44
I changed my logic as below

Expand|Select|Wrap|Line Numbers
  1. public static final Logger getLogger( final String sLoggerName )throws IOException
  2. {
  3.   final Logger logger = LogManager.exists(sLoggerName);
  4.         if(logger != null)
  5.         {
  6.             return logger;
  7.         }
  8.  
  9.          Logger newLogger = LogManager.getLogger(sLoggerName);
  10.         //Logger is not exists so create appender for logger
  11.         String sFileName = m_sFilePath + sLoggerName + m_sExt;
  12.         RollingFileAppender appender = new RollingFileAppender(m_layout, sFileName, true );
  13.         appender.setMaxBackupIndex( m_iMaxBufferIndex );
  14.         appender.setMaxFileSize( m_sLogMaxFileSize );
  15.         newLogger.addAppender( appender );
  16.         newLogger.setLevel( m_logLevel );
  17.         newLogger.setAdditivity(false);
  18.        return newLogger;
  19. }
  20.  
still logger writing logs in wrong files, can any buddy please help me and suggest what's wrong with my logic.

My server having 64 threads ( ie 64 logger ) and it is running on "8 core" machine, is this a problem as it is hightly multithreaded ????

Abhinay
Feb 3 '09 #2
umbr
9
Hi Abhinay!
For logging into separate files you need cofigure different loggers instead of appenders. Look, like this:
Expand|Select|Wrap|Line Numbers
  1. log4j.rootLogger=DEBUG, ROOT
  2. log4j.appender.ROOT=... #configure main(root) appender
  3.  
  4. # configure logger for com.foo.1 category
  5. log4j.logger.com.foo.1=INFO, ONE
  6. log4j.appender.ONE=... #configure appender ONE
  7.  
  8. # configure logger for com.foo.2 category
  9. log4j.logger.com.foo.2=INFO, TWO
  10. log4j.appender.TWO=... #configure appender TWO
  11.  
Feb 16 '09 #3
Abhinay
44
Thank you umbr for your suggestion,

but what should I do if I want to create logger at runtime ?

My thread count ( hence logger ) are configurable hence I can't decide and configure logger in advance, I must have to create logger at runtime.
Feb 19 '09 #4
umbr
9
Hi Abhinay!
Yes, for indetermined categories you must override Logger class for your purposes(may be with custom appender), which will dynamically create new log files.
Expand|Select|Wrap|Line Numbers
  1.     String filename = ... // generate name of new logfile
  2.     Logger logger = new MyLogger(filename);
  3.     // do as you need with logger
  4.     ...
  5.  
Feb 19 '09 #5
Abhinay
44
Thank all of you for helping me, finally I got the root cause of problem.
This problem is due to static layout which I was sharing with all logger appenders.


---Abhinay
Feb 24 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Aaron Boxer | last post by:
Hello, My application is using a jar file MyJar.jar that uses log4j (I have the latest release of log4j). I have put both MyJar.jar and log4j.jar in a directory in my classpath. I have also...
1
by: Adi | last post by:
Hi, I have a web application (using J2EE) which runs on 3 tomcats (4.0) for Load Balancing. I have recently started to use the Log4j in this application. Currently the logs for this application...
1
by: Ragavendra BC | last post by:
Log4j Issue ----------- Hi, I am facing a problem related to Log4j... The problem is such that I have two different Log4j files one in ..properties format and the other in the .xml format.....
4
rsrinivasan
by: rsrinivasan | last post by:
Hi, I am using log4j to create log message. I want to write the log message to database. So i configured the log4j.properties files like this... # Database Configuration...
0
by: Hari2349 | last post by:
Hai, friends i used the log4j.properties file like as below...... log4j.rootLogger=VERBOSE, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender ...
2
by: Hari2349 | last post by:
Regarding to log4j in java program..Plz help me... -------------------------------------------------------------------------------- Hai, friends i used the log4j.properties file like as...
1
by: Kedar Kachare | last post by:
How to use Log4j to create log files using file appender in a Servlet? Also, how to make that Servlet read the log4j.properties file? I have tried following: My web.xml( I use Eclipse europa)...
0
by: Mikhail Teterin | last post by:
Hello! I'm trying to use log4j's SMTPAppender to get warnings and errors reported by our application via e-mail (in addition to having ALL messages saved into a file via RollingFileAppender). ...
1
by: nagk9 | last post by:
Hi , I am using log4j in my application. I have the problem with log4j.properties file . It will automatically appends another logger file from the current logger which i am using for the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.