log4j to a file

Six Stars

log4j to a file

Hi, 
I need help to write log4j logging information to a file. By default log4J is writing on console. How should I configure, and where should I configure If I want log4J to write logging information to a file like " c:/log.txt ".

 
Thanks,
Abhi529

Moderator

Re: log4j to a file

Hello,

Please take a look at this related topic:https://community.talend.com/t5/Design-and-Development/Issue-with-tRest/m-p/31782

You can do this with a little Java code in a tJava component at the beginning of your Job or use a custom component tredirectOutput.

Let us know if it is Ok with you.

Best regards

Sabrina

 

 

--
Don't forget to give kudos when a reply is helpful and click Accept the solution when you think you're good with it.
Six Stars

Re: log4j to a file

Hi xdshi,

thanks for your answer but it is not work for me.

I have added the following code into log4j.xml.....then still i am unable to get the the log4j file in reqired location...

 <appender name="DailyFileAppender" class="org.apache.log4j.DailyRollingFileAppender">
		<param name="DatePattern" value="'.'yyyy-MM-dd"/>
		<param name="File" value="c:/log/myjob.txt "/>
		<param name="Append" value="true"/>
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %-5p %c{2} - %m%n"/>
		</layout>
	</appender>

Please find below image...

Capture_log4j.JPG

Thanks,

Abhi529

Tags (1)
Moderator

Re: log4j to a file

Hi,

You added code into log4j.xml?

Have you tried to use this custom component tredirectoutput which is used to redirect all the message printed on the console to a file. to see if it works?

Best regards

Sabrina

 

--
Don't forget to give kudos when a reply is helpful and click Accept the solution when you think you're good with it.
Moderator

Re: log4j to a file

Hello,

Is there any update for your issue? Feel free to post your issue here.

Best regards

Sabrina

--
Don't forget to give kudos when a reply is helpful and click Accept the solution when you think you're good with it.
Five Stars

Re: log4j to a file

Hi@xdshi ,I've tried your method too, but it doesn't work.

 

Moderator

Re: log4j to a file

Hello,

Which section will block you? Are you able using tJava component to save your output in a file?

Best regards

Sabrina

--
Don't forget to give kudos when a reply is helpful and click Accept the solution when you think you're good with it.
Five Stars

Re: log4j to a file

Thank you for your answer.But how do I use the tJava component  to output the content of log4j?

Four Stars

Re: log4j to a file

Hi all,

 

You need to complete with this at the end :

 

    <!-- Root Logger -->  
	<root>
		<priority value ="warn"/>
		<appender-ref ref="CONSOLE"/>
		<appender-ref ref="DailyFileAppender"/>
	</root>

Here my complete configuration : 

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
  
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/' >
  
  	<!--  CONSOLE appender  -->
	<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
		<layout class="org.apache.log4j.PatternLayout">
		 	<param name="ConversionPattern"  
		  		value="[%-5p]: %c - %m%n" />
		</layout>
	</appender>

 <appender name="DailyFileAppender" class="org.apache.log4j.DailyRollingFileAppender">
		<param name="DatePattern" value="'.'yyyy-MM-dd"/>
		<param name="File" value="c:/log/myjob.txt "/>
		<param name="Append" value="true"/>
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %-5p %c{2} - %m%n"/>
		</layout>
</appender>
	
	<!-- logstash appender -->
    <!-- <appender name="LOGSTASH" class="org.apache.log4j.net.SocketAppender">
        <param name="RemoteHost" value="localhost"/>
        <param name="Port" value="8052"/>
    </appender> -->
	
	<!-- Spring Logger -->  
	<logger name="log4j.logger.org.springframework">  
        <level value="WARN" />  
        <appender-ref ref="CONSOLE"/>
    </logger>
    
    <!-- throwableRenderer <throwableRenderer class="org.apache.log4j.EnhancedThrowableRenderer"/>--> 
	
    <!-- Root Logger -->  
	<root>
		<priority value ="warn"/>
		<appender-ref ref="CONSOLE"/>
		<appender-ref ref="DailyFileAppender"/>
	</root>
</log4j:configuration>
Six Stars

Re: log4j to a file

I think your appender name has to be FILE not DailyFileAppender

2019 GARNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch Now

Best Practices for Using Context Variables with Talend – Part 2

Part 2 of a series on Context Variables

Blog

Best Practices for Using Context Variables with Talend – Part 1

Learn how to do cool things with Context Variables

Blog

Migrate Data from one Database to another with one Job using the Dynamic Schema

Find out how to migrate from one database to another using the Dynamic schema

Blog