473,811 Members | 3,521 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Issue regarding log4j

44 New Member
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.getLogge r(...) function return wrong logger object.

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

where:
sLoggerName : Current thread name ( Thread.currentT hread.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 4116
Abhinay
44 New Member
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 New Member
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 New Member
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 New Member
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 New Member
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
180329
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 put the file "log4j.properties" into this same directory. My log4j.properties file reads:
1
5989
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 is of the tomcat's(System.out.println...) - and because there are 3 tomcats there are 3 logs. The log4j writes to one file only. My question is: How can I tell in the log4j log from which server the request came
1
5607
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.. and their source code is like this
4
7426
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 log4j.appender.dbase =org.apache.log4j.jdbc.JDBCAppender log4j.appender.dbase.layout =org.apache.log4j.PatternLayout log4j.appender.dbase.driver =com.microsoft.sqlserver.jdbc.SQLServerDriver log4j.appender.dbase.URL ...
0
1257
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 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern= %d
2
2008
by: Hari2349 | last post by:
Regarding to log4j in java program..Plz help me... -------------------------------------------------------------------------------- Hai, friends i used the log4j.properties file like as below...... log4j.rootLogger=VERBOSE, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppe nder
1
5524
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) <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"...
0
5724
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). It all works, except for one thing -- every warning/error arrives in its own e-mail message... The BufferSize parameters appears intended to regulate this, but no matter
1
1812
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 current application and also it logs or merging other logger content of other applications in the current logger. I don't want to print other logger messages from the current logger. Is there any way to restrict the other logger messages of other...
0
9726
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10647
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10395
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10130
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7667
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6887
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.