This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Logging Package Specific logs using LOG4J

 
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone,
I am stuck in something urgent.I want to do logging for some functionality.For example FTP.Now all my FTP classes are not in same package.

i.e. com.xyz.ftp contains 3 files of FTP plus 2 other files of seperate functionality
com.xyz.utilities has 2 utility files used by FTP.

Now since i want to log only FTP related files, so i need to do package specific logging and that too for specific classes in a package.

How this can be acheived using LOG4J ? I am helpless.If i do like this:

<appender name="MyAppender"
class="org.apache.log4j.DailyRollingFileAppender" >
<param name="File" value="./log/MyFile" />
<param name="Append" value="true"/>
<param name="DatePattern" value="'.'dd-MM-yyyy.hh-mm-ss'.log'" />
<param name="Threshold" value="DEBUG" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="[%-12d{HH:mm:ss:SS} %-5p %c{1}] %m%n"/>
</layout>
</appender>


<logger name="com.xyz.ftp"
additivity="false">
<level value="debug"/>
<appender-ref ref="MyAppender"/>
</logger>

then it would log for all other files in FTP package as well, so how to filter out that logging ?

Any quick solution would be appreciated.

Thanks in advance,
Saurabh
 
Sheriff
Posts: 24658
58
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't have to use the class name as the logger name. Try something like one of these:
 
Saurabh Agrawal
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Paul Clapham:
You don't have to use the class name as the logger name. Try something like one of these:



I could not get you. If i dont use class name as logger name then what difference is it going to make ?

I am puzzled.Does that mean i need to change my code of instantiating a logger and not properties of log4j.xml?

Will that ensure that only specific classes in a package are logged in a file?
Please clarify and thanks for the help.

Let me know what change do i need to make and where ?
Saurabh
 
Paul Clapham
Sheriff
Posts: 24658
58
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. The second step is to change your configuration like this:To reiterate, your problem is that you want a certain set of classes to send their logs to a certain logger. And there is no meaningful order to those classes. The solution is to not use the class name as the name of the logger. And log4j does not require you to do that. So don't do it.

And yes, you will have to change the code in those classes to write to a particular logger. Again, the code that gets the logger is not required to use the class name as the name of the logger.
 
Saurabh Agrawal
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Paul Clapham:
Okay. The second step is to change your configuration like this:To reiterate, your problem is that you want a certain set of classes to send their logs to a certain logger. And there is no meaningful order to those classes. The solution is to not use the class name as the name of the logger. And log4j does not require you to do that. So don't do it.

And yes, you will have to change the code in those classes to write to a particular logger. Again, the code that gets the logger is not required to use the class name as the name of the logger.




Thanks,It worked but when i used ftplog in the log4j.xml file and instantitated the logger by this name, then logs were getting created but i was unable to print the class name in the log.Name of the logger i.e. "FTPLOG" was getting logged in.

This means i was unable to find the class name which is essential aspects of the logging.

Please let me know what can be done?

Thanks in advance,
Saurabh
 
Paul Clapham
Sheriff
Posts: 24658
58
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recall that when I first answered this question, I said "Try something like one of these". Well, you did. Now try something like the other one.
 
Saurabh Agrawal
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Paul Clapham:
Recall that when I first answered this question, I said "Try something like one of these". Well, you did. Now try something like the other one.



Cool.The second solution of assigning the "ftplog."+ classname worked.But just a slight question on this.If i have a class which is utility class and would be logged in 2 differnt files.Then in that case how should i get the logger ?

If i instantiate the logger as "ftplog."+ classname for this utility class then it would log in a particular file butu not in multiple files.

Thanks in advance,
 
Saurabh Agrawal
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Paul Clapham:
Recall that when I first answered this question, I said "Try something like one of these". Well, you did. Now try something like the other one.



Hi Paul,
I am stuck in one thing.Suppose i have a utility class which is used in my 2 different packages and i am doing package specific logging for specified classes using:
ftplog = Logger.getLogger("ftplog." + this.getClass().getName());

Now this utility class's logger would log files in the ftplog file's appender but since this is common utility and when used from differnt class then it wont log to the file pointed by different appender.

So how can this be done ? This would restrict log usage of utility class to a particualar class only.

Help me.
saurabh
 
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!