Results 1 to 8 of 8
  1. #1
    tusharbanne is offline Member
    Join Date
    Apr 2017
    Location
    Bangalore
    Posts
    4
    Rep Power
    0

    Default Creating Flow specific log files

    Hi,
    I am using log4j in my project for logging purpose. Initially I had just one file where the entire log used to get generated.
    But Now I am feeling that there should be flow specific log files which will help debug issues.

    I am working with spring-MVC project which has CRUD operations. The CRUD operations are distributed over Controller class, Validators, Business logic and DAO operations.

    I want to Create separate log files for each flow of Create, Retrieve, Update and Delete.

    Can this be achieved using log4j?

    If Yes How do I achieve it.

    Thanks.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Creating Flow specific log files

    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    tusharbanne is offline Member
    Join Date
    Apr 2017
    Location
    Bangalore
    Posts
    4
    Rep Power
    0

    Default Re: Creating Flow specific log files

    Thanks, It Solved my problem.
    Now the next problem that I am facing is, how do I still get class names in the Log file.

    //previous logger initialization
    final static Logger logger = Logger.getLogger(Hello.class);

    //current logger initialization
    Logger admin = Logger.getLogger("admin");

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Creating Flow specific log files

    If you're using a recent version of Log4J (and you should), then you can create four appenders and assign classes to it. Following example creates logfiles in %USER_HOME%/myapplication/logfiles/ called application.log for normal operations, create.log for all log statements from the com.mypackage.crud.MyCreateClass class, etc.

    You can now use Logger log = LogManager.getLogger(MyCreateClass.class); (The XML should match the packagename or classname)

    Java Code:
    <?xml version="1.0" encoding="UTF-8" ?>
    <Configuration status="WARN">
    	<Properties>
    		<Property name="log-path">${sys:user.home}/myapplication/logfiles/</Property>
    	</Properties>
    
    	<Appenders>
    		<Console name="CONSOLE" target="SYSTEM_OUT">
    			<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%-30c{1}] %m%n"/>
    		</Console>
    		<RollingFile name="NORMAL" fileName="${log-path}/application.log" filePattern="${log-path}/application.log.%d{yyyy-MM-dd}.log">
    			<PatternLayout>
    				<pattern>%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%-30c{1}] %m%n</pattern>
    			</PatternLayout>
    			<Policies>
    				<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
    			</Policies>
    		</RollingFile>
    		<RollingFile name="FILE_CREATE" fileName="${log-path}/create.log" filePattern="${log-path}/create.log.%d{yyyy-MM-dd}.log">
    			<PatternLayout>
    				<pattern>%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%-30c{1}] %m%n</pattern>
    			</PatternLayout>
    			<Policies>
    				<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
    			</Policies>
    		</RollingFile>
    		<RollingFile name="FILE_DELETE" fileName="${log-path}/delete.log" filePattern="${log-path}/delete.log.%d{yyyy-MM-dd}.log">
    			<PatternLayout>
    				<pattern>%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%-30c{1}] %m%n</pattern>
    			</PatternLayout>
    			<Policies>
    				<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
    			</Policies>
    		</RollingFile>
    	</Appenders>
    
    	<Loggers>
    		<logger name="com.mypackage.crud.MyCreateClass" level="WARN" additivity="false">
    			<AppenderRef ref="FILE_CREATE"/>
    		</logger>
    		<logger name="com.mypackage.crud.MyDeleteClass" level="WARN" additivity="false">
    			<AppenderRef ref="FILE_DELETE"/>
    		</logger>
    
    		<Root level="WARN">
    			<AppenderRef ref="NORMAL"/>
    			<AppenderRef ref="CONSOLE"/>
    
    		</Root>
    	</Loggers>
    </Configuration>
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    tusharbanne is offline Member
    Join Date
    Apr 2017
    Location
    Bangalore
    Posts
    4
    Rep Power
    0

    Default Re: Creating Flow specific log files

    I am using log4j 1.2.9

    In this case I need to add every class in the properties file respective to the appender.
    This will fill up my log4j.properties file as I have lots of Class files in my project.

    Also can u please tell me the syntax for properties file?

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Creating Flow specific log files

    You can also use the packagename:
    <logger name="com.mypackage.crud" level="WARN" additivity="false">
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  7. #7
    tusharbanne is offline Member
    Join Date
    Apr 2017
    Location
    Bangalore
    Posts
    4
    Rep Power
    0

    Default Re: Creating Flow specific log files

    There lies the problem, the class files from the same package aren't supposed to write to the same file.

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Creating Flow specific log files

    Then you have no choice but to create different appenders for each class file.
    Last edited by SurfMan; 04-25-2017 at 02:52 PM. Reason: Typo
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Replies: 6
    Last Post: 05-15-2013, 09:30 PM
  2. Replies: 0
    Last Post: 01-13-2013, 03:21 AM
  3. Replies: 6
    Last Post: 12-14-2011, 03:42 PM
  4. Creating Jar Files with functioning input files
    By appleLove in forum NetBeans
    Replies: 1
    Last Post: 04-10-2011, 10:37 PM
  5. Replies: 0
    Last Post: 01-27-2010, 03:52 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •