Tech I Enjoy Logo

Custom Search




  Home >> Miscellaneous

Using Log4j How to write different types of log output to different files,
suppose normal debug information stored in a seperate file and more specific
process related audit information stored in a separate log file?

Most probable answer (Open for discussion by Reply) :

There can be many ways of doing this, one of the solution is by defining 
log4j.properties in such a way so as to have two different Appenders, 
with two different log files.

This is statically defined configuration, that is defining two different 
Loggers as well, as follows:Here I am using Apache Log4J version 1.2.x.

1. Define rootLogger=DEBUG as

   log4j.rootLogger=DEBUG

2. As two different types of loggers, here I define
   these as DEFAULT_LOGGER and PROCESS_LOGGER,

log4j.logger.DEFAULT_LOGGER=INHERIT, A1 log4j.logger.PROCESS_LOGGER=INHERIT, A2 These two custome loggers are to be inherit from the root logger, and two appenders A1 and A2 are attached to each of these loggers. 3. Now these two appenders A1 and A2 are to be set with respective properties such as type of layout, patern and file name etc. log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender log4j.appender.A1.File=app.log log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n log4j.appender.A1.DatePattern='.'yyyy-MM-dd-HH-mm log4j.appender.A2=org.apache.log4j.FileAppender log4j.appender.A2.File=audit_process.log log4j.appender.A2.layout=org.apache.log4j.PatternLayout log4j.appender.A2.layout.ConversionPattern=%d [%t] %-5p %c - %m%n Here default logger has daily rolling file appender, that means with every system date change, it is going to create a separate log file with complete date with timestamp. And process logger has a simple file appender, with a specific pattern. In order to test this log4j properties file, I have some sample Java code as follows: TestService.java import org.apache.log4j.Logger; public class TestService { Logger logger1 = Logger.getLogger("DEFAULT_LOGGER"); Logger logger2 = Logger.getLogger("PROCESS_LOGGER"); public void testMethod() { logger1.debug("Default Logger"); logger2.debug("Process Logger"); } } and the test client for calling this demo service as follows: TestMultiLogger.java public class TestMultiLogger { public TestMultiLogger() { new TestService().testMethod(); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new TestMultiLogger(); } } As depicted in the TestService class file, here we have to call appropriate logger for logging either default or process logger. One question could arise that generally we pass class name as the argument to the Logger.getLogger method?? but the concern class name and method can be printed in the log file with appropriate settings in log4j.properties file as follows: .... .... log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %C-%M %c - %m%n .... .... Hope this helps, If you want to share some more suggestions or ways of doing this, please write to us by replying to this write up. After review and required changes made, if any, it will be published in this page. If anything missed out , please let me know at techienjoy at yahoo . com
Using Quartz Scheduler Example :
Example on how to use Quartz Scheduler.
Using Apache Commons Log With Example :
Example using Apache commons log 
with code and explained
JSF example on validation :
JSF Validation with example with source code 
on Java Platform.
Android Image Gradient Merge :
Example using Images and Gradient Shape using
Android Platform.
Android Tab View Example :
Example on Android Tab View
explained with a very simple scenario
and appropriate screens captured and shown.
DOJO Dialog Example :
Example on using DOJO Dialog
explained with a very simple scenario
JDBC Transaction Isolation Levels :
A short write-up on JDBC Transaction
Isolation showing ways to achieve
various Isolation levels using JDBC.
Android TimePickerDialog Example :
Example on Android TimePickerDialog
explained with a very simple scenario
and appropriate screens captured and shown.
List of Examples on Various Technologies :
List of Examples on Various Technologies and Frameworks.
Android ImageView Example :
Example on using ImageView using 
Android Platform. A very simple to setup
and see it working.
Android Menu and MenuItem Example :
Example using Menu and MenuItem using Android Platform 
with code and explained
Example of using Log4J Part 1 :
Log4j example with source code on Java Platform.
Android ListView Example :
Example on Android ListView 
explained with a very simple scenario
whereby showing folder and files with
structure and appropriate screens 
captured and shown.
Android Example on Expandable List :
Example on using Expandable ListView
on Android Platform.A step by step source code
explained.
Google GWT Example :
Example using GWT and some design patterns and various
ways of implementing this example.
Android Sensors Example :
Example on Android Sensors Listed and
explained with a very simple scenario 
and article with appropriate screens 
captured and shown.
Example of using Mule ESB JMS Transport :
Example of using Mule ESB JMS Transport with simple
to explain source code.
Wizard Framework using Java Platform :
Example using Custom Wizard Framework 
with code and explained
JSF example with source code :
JSF example with source code on Java Platform.
Example using Tag Library :
Example on how to code and use
Custom Tag Library on Java Platform.
JSF example with source code :
JSF example of Tags and checkboxes 
with source code on Java Platform.
Android Gallery Example Enhanced :
Example on Android Gallery View
explained with a very simple scenario
and appropriate screens captured and shown.
Example of using Log4J Part 2 :
Log4j example with source code on Java Platform.
Android Example on Expandable ListView :
Example on using Expandable ListView
on Android Platform.A step by step source code
explained.
Android Layout Example :
Android Example on using Layout
with source code Explained.
JSF example with source code :
JSF example of Tags and Data Table 
with source code on Java Platform.
Android Gallery with SurfaceView :
Example showing Android Gallery
with SurfaceView and Spinner
Web Load Test with example :
Example using Load test functionalities 
with code and explained
Android Example on Downloading AnyFormat :
Example on ways to download any file with
any format using Android Platform.
Android ListView Example :
Example on Android ListView and
explained with a very simple scenario 
and article with appropriate screens 
captured and shown.
Example of using Mule ESB File Transport :
Example of using Mule ESB File Transport with simple
to explain source code.
DOJO Tree Widget Example :
Example on using DOJO Tree Widget
explained with a very simple scenario
Android ListView Example :
Example on Android List View
explained with a very simple scenario 
and article with appropriate screens 
captured and shown.
JSF example with source code :
JSF example of Tags and SelectBoxes 
with source code on Java Platform.
JSF example on Resource Bundle :
JSF example of Resource Bundle with source code 
on Java Platform.
Android DatePickerDialog Example :
Example on Android DatePickerDialog
explained with a very simple scenario
and appropriate screens captured and shown.
Android Examples :
List of ANDROid examples
with source code and output
screens captured and shown.
Android Gallery Example :
Example on Android Gallery View
explained with a very simple scenario
and appropriate screens captured and shown.
Android Text to Speech Example :
Android Example on using Text
2 Speech conversion explained with
source code Explained.
JSF example with source code :
JSF example of Tags and Code Walk-through 
with source code on Java Platform.
Using Different Logger Files :
Example on using different log files 
using Apache Log4j Framework.


References :
Tags: TabHost and TabActivity Example on Android Platform
Tags: ListView Example on Android Platform
Tags: android sensors list
Tags: android listview example
Tags: android imageview example
Tags: Android example download any file sourcecode
Tags: android expandable list dynamically created example
Tags: android expandable list example
Tags: Android Gallery surfaceviews spinner
Tags: Android example download any file sourcecode
Tags: Android Layout Example
Tags: Android Text To Speech Example

Tags: DOJO Example Dialog
Tags: DOJO Example Tree Widget
Tags: different logger file log4j
Tags: JDBC Transaction isolation
Tags: event handling java code
Tags: example quartz scheduler
Tags: example tag library web application
Tags: Flex
Tags: index
Tags: inmemory image creation java awt
Tags: JSF Example Main
Tags: JSF Example Tags CheckBoxes
Tags: JSF Example Tags dataTable
Tags: JSF Example Tags SelectBoxes
Tags: JSF Example Tags Walkthrough
Tags: JSF Example Validation
Tags: JSF Resource Bundle
Tags: log4j example 1
Tags: log4j example
Tags: Miscellaneous
Tags: Mule ESB File Transport
Tags: Mule ESB JMS Transport
Tags: stream download batch
Tags: sychronized block wait notify
Tags: thread wait notify example
Tags: using apache commons log
Tags: web load test
Tags: Wizard Framework Idea Java



DISCLAIMER :
The content provided in this page is not warranted and/or guaranteed by techienjoy.com. 
techienjoy.com is not liable for any negative consequences that may result/arise from 
implementing directly/indirectly any information covered in these pages/articles/tutorials.

All contents of this site is/are written and provided on an "AS IS" basis,
without WARRANTIES or conditions of any kind, either express or implied, including, without
limitation, merchantability, or fitness for a particular purpose. You are solely responsible
for determining the appropriateness of using or refering this and assume any risks associated
with this.

In spite of all precautions taken to avoid any typo in these pages, there might be some 
issues like grammatical mistakes and typos being observed in these pages, techienjoy.com
extends sincerest apologies to all our visitors for the same.



Android Examples || Android Examples

© Copyright 2010-2012, TECHIENJOY, All Rights Reserved.      Privacy Policy     Disclaimer & Terms & Conditions