add timestamp in the log file on daily basis - apache

I want to add the timestamp at the end of my log file on the daily basis.
Means for every day the log file should look like
test.2013-01-10.log
My new log.properties file is below.Please help
log4j.rootLogger=info,myapp
log4j.appender.myapp=org.apache.log4j.DailyRollingFileAppender
log4j.appender.myapp.ImmediateFlush=true
log4j.appender.myapp.DatePattern='.'yyyy-MM-dd-HH
log4j.appender.myapp.layout.ConversionPattern=%d{yyyy-MM-dd}%m%n
log4j.appender.myapp.file=${catalina.base}/logs/myapplog/test
log4j.appender.myapp.DatePattern='_'yyyy-MM-dd
#log4j.appender.myapp.MaxFileSize=999MB
#log4j.appender.myapp.MaxBackupIndex=20
log4j.appender.myapp.layout=org.apache.log4j.PatternLayout
log4j.appender.consoleAppender = org.apache.log4j.ConsoleAppender
log4j.appender.consoleAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern=%m%n
log4j.logger=info,stdout,myapp
log4j.logger.org.hibernate=warn

Try use DailyRollingFileAppender instead of RollingFileAppender in your configuration
EDIT :
Try this. It will generate test.log and when your computer date changed, it will generate new test.log and test.logyyyy-mm-dd
log4j.properties :
log4j.rootLogger=info,A1
log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A1.ImmediateFlush=true
log4j.appender.A1.DatePattern='.'%d{yyyy-mm-dd}
log4j.appender.A1.layout.ConversionPattern=%d{yyyy-MM-dd}%m%n
log4j.appender.A1.File=c:/test.log
log4j.appender.A1.DatePattern='.'yyyy-MM-dd
#log4j.appender.A1.MaxFileSize=999MB
#log4j.appender.A1.MaxBackupIndex=20
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.consoleAppender = org.apache.log4j.ConsoleAppender
log4j.appender.consoleAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern=%m%n
log4j.logger=info,stdout,A1
log4j.logger.org.hibernate=warn
Code :
import org.apache.log4j.Logger;
public class test {
public static void main(String[] args) {
Logger barlogger = Logger.getLogger(test.class);
barlogger.info("test");
}
}
UPDATE
If you want to change your log to .txt, change this:
log4j.appender.A1.File=${catalina.base}/logs/test.log
to:
log4j.appender.A1.File=${catalina.base}/logs/test.txt

Related

How to send extent report in email to stackholders after running all the test cases in cucumber?

I want to send an email with extent report attachment that I have generated. I am using cucumber. Currently report is generated using latest timestamp with below name
D:\DAAutomation1\NewFeature1\output\10062021_071218798\Report_10062021_071218798.html
Now I want to send this dynamically generated report in email. I am trying to send using the below code in SequentialRunnerTestbut it is not working.
How can I attach a dynamically generated report which stored in a dynamically generated folder?
From which location I need to call this code?
#BeforeClass
public static void Setup() {
if (CustomFormatter.getReportInstance() == null) {
Date d = new Date();
String today = new SimpleDateFormat(Constants.SCREENSHOT_SDF).format(d);
String reportName = String.format("Report_%s%s", today, Constants.HTML_EXTENSION);
File dir = new File(today);
dir = new File(Constants.REPORT_PATH + dir);
if (!dir.exists()) {
dir.mkdir();
Variables.reportFolderName = dir;
}
reportPath = new File(dir + "/" + reportName);
File folderPath = new File(dir + "/");
CustomFormatter.initiateCustomFormatter(reportPath, folderPath);
File extentConfig = new File(Constants.CONFIG_FILES_URI + Constants.EXTENT_FILE);
CustomFormatter.loadConfig(extentConfig);
CustomFormatter.addSystemInfo("user", System.getProperty("user.name"));
CustomFormatter.addSystemInfo("os", System.getProperty("os.name"));
CustomFormatter.addSystemInfo("browser", CONFIG.getProperty("browser"));
CustomFormatter.addSystemInfo("Tenant", CONFIG.getProperty("application.url"));
} else {
CustomFormatter.initiateCustomFormatter();
}
#AfterClass
public static void SendEmail() throws EmailException {
// Create the attachment
EmailAttachment attachment = new EmailAttachment();
attachment.setPath(System.getProperty("user.dir")+"output/folderPath/"+reportPath);
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription(" Test Execution Report");
attachment.setName("Automation Test Execution Report");
// Create the email message
MultiPartEmail email = new MultiPartEmail();
email.setHostName("smtp.gmail.com");
email.setSSLOnConnect(true);
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator("xyz#gmail.com", "xyz#123"));
email.addTo("xyz#gmail.com", "Test");
email.setFrom("xyz#gmail.com", "Me");
email.setSubject("Automation Test Execution Report");
email.setMsg("Automation Test Execution Report");
// add the attachment
email.attach(attachment);
// send the email
email.send();
}
please write seperate simple java program that should be executed after your cucumber run.
After the complete execution only, you will see the latest report in your target folder. your secondary program should pick the report from the target folder and mail to them.
In My case,
I have written separate java program and JAR packed that will do following actions,
Zip screenshot, css and html report from target folder,
Move them to separate folder with current date and time to identify
Then mail the zip folder
My Execution like,
Created a .bat/sh file
added my cucumber execution
added secondary program execution as JAR execution
mvn test -DCucumber.Options="--tags #temp"
java -jar ZippingAndEmailing.jar [reportLocation] [targetlocation] [emailReciptents]
java -jar ZippingAndEmailing.jar target/cucumber Results jayanthbala1993#gmail.com
From which location I need to call this code?
You have to call that under #AfterClass as you want to send report after executing all tests.
#AfterClass
public static void sendReport() {
SendReport sendReport = new SendReport();
sendReport.triggerMail("Report", "\\NewFeature1\\output\\10062021_071218798\\Report_10062021_071218798.html);
}
How can I attach a dynamically generated report which stored in a
dynamically generated folder
public class SendReport{
public String[] ToAdresses = { "nandan#gmail.com"
,"nandan2#gmail.com"
public void triggerMail(String reportName, String reportPath)
throws IOException, AddressException, MessagingException {
Properties sysmProp = System.getProperties();
sysmProp.put("mail.smtp.starttls.enable", "true");
sysmProp.put("mail.smtp.host", host);
sysmProp.put("mail.smtp.user", from);
sysmProp.put("mail.smtp.password", password);
sysmProp.put("mail.smtp.port", "587");
sysmProp.put("mail.smtp.auth", "true");
/*Create session object*/
Session session = Session.getInstance(sysmProp, null);
/*Create MimeMessage object and add recipients */
MimeMessage message = new MimeMessage(session);
/* Setting the string value type as address */
InternetAddress[] recipients = new InternetAddress[ToAdresses.length];
for (int i = 0; i < ToAdresses.length; i++) {
recipients[i] = new InternetAddress(ToAdresses[i]);
}
/* Adding the recipients to the message object. */
for (int j = 0; j < ToAdresses.length; j++) {
message.addRecipient(Message.RecipientType.TO, recipients[j]);
}
message.setSubject("Test report");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Body of email.")
/* Adding the attachment to the mail. */
File file = new File(System.getProperty("user.dir") + reportPath);
BodyPart messageBodyPart_2 = new MimeBodyPart();
DataSource source = new FileDataSource(file.getAbsolutePath());
messageBodyPart_2.setDataHandler(new DataHandler(source));
messageBodyPart_2.setFileName("Test_" + reportName + ".html");
/* Clubbing the subject of mail. */
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
multipart.addBodyPart(messageBodyPart_2);
message.setContent(multipart);
/* Triggers mail. */
Transport.send(message);
}
}

Data not correctly read from hadoop using Filesystem API

I am trying to read a file from hadoop using filesystem API, I am able to connect hadoop and read the file , however file read contains garbled characters.
Below is the code:
public class HdfsToInfaWriter{
public static void main(String[] args)
{
//FileUtil futil;
String hdfsuri=args[0];
//String src=args[1];
String localuri=args[1];
String hdusername=args[2];
byte[] buffer=new byte[30];
char c;
Configuration conf=new Configuration();
conf.addResource(new Path("file:///etc/hadoop/conf/core-site.xml"));
conf.addResource(new Path("file:///etc/hadoop/conf/hdfs-site.xml"));
conf.set("hadoop.security.authentication", "kerberos");
conf.set("fs.defaultFS",hdfsuri);
conf.set("fs.hdfs.impl",org.apache.hadoop.hdfs.DistributedFileSystem.class.getName());
conf.set("fs.file.impl",org.apache.hadoop.fs.LocalFileSystem.class.getName());
//futil.copy(srcFS, src, dst, deleteSource, conf)
try {
UserGroupInformation.setConfiguration(conf);
UserGroupInformation.loginUserFromKeytab("**************",
"********************");
}catch(IOException e){
e.printStackTrace();
}
System.setProperty("HADOOP_USER_NAME",hdusername);
System.setProperty("hadoop.home.dir","/");
FSDataInputStream in1 = null;
try{
FileSystem fs = FileSystem.get(URI.create(hdfsuri),conf);
Path hdfsreadpath=new Path(hdfsuri);
CompressionCodecFactory factory = new CompressionCodecFactory(conf);
System.out.println("the class for codec is " +factory.getCodec(hdfsreadpath));
File src1=new File(localuri);
System.out.println("before copy");
FileUtil.copy(fs, hdfsreadpath, src1, false, conf);
}}}
When i use hdfs command hdfs dfs -cat /bigdatahdfs/datamart/trial.txt, the data in file is a simple text file.
But when I use the command cat /home/trial1.txt and copy file to local system, the output is as below:
▒▒▒1K▒;▒▒
=▒<▒▒▒&▒▒▒
NOTE:- i have tried using IOUtils API also, output is the same.

How to set log filename in flume

I am using Apache flume for log collection. This is my config file
httpagent.sources = http-source
httpagent.sinks = local-file-sink
httpagent.channels = ch3
#Define source properties
httpagent.sources.http-source.type = org.apache.flume.source.http.HTTPSource
httpagent.sources.http-source.channels = ch3
httpagent.sources.http-source.port = 8082
# Local File Sink
httpagent.sinks.local-file-sink.type = file_roll
httpagent.sinks.local-file-sink.channel = ch3
httpagent.sinks.local-file-sink.sink.directory = /home/avinash/log_dir
httpagent.sinks.local-file-sink.sink.rollInterval = 21600
# Channels
httpagent.channels.ch3.type = memory
httpagent.channels.ch3.capacity = 1000
My application is working fine.My problem is that in the log_dir the files are using some random number (I guess its timestamp) timestamp as by default.
How to give a proper filename suffix for logfiles ?
Having a look on the documentation it seems there is no parameter for configuring the name of the files that are going to be created. I've gone to the sources looking for some hidden parameter, but there is no one :)
Going into the details of the implementation, it seems the name of the file is managed by the PathManager class:
private PathManager pathController;
...
#Override
public Status process() throws EventDeliveryException {
...
if (outputStream == null) {
File currentFile = pathController.getCurrentFile();
logger.debug("Opening output stream for file {}", currentFile);
try {
outputStream = new BufferedOutputStream(new FileOutputStream(currentFile));
...
}
Which, as you already noticed, is based on the current timestamp (showing the constructor and the next file getter):
public PathManager() {
seriesTimestamp = System.currentTimeMillis();
fileIndex = new AtomicInteger();
}
public File nextFile() {
currentFile = new File(baseDirectory, seriesTimestamp + "-" + fileIndex.incrementAndGet());
return currentFile;
}
So, I think the only possibility you have is to extend the File Roll sink and override the process() method in order to use a custom path controller.
For sources you have execute commands to tail and pre-pend or append details, based on shell scripting. Below is a sample:
# Describe/configure the source for tailing file
httpagent.sources.source.type = exec
httpagent.sources.source.shell = /bin/bash -c
httpagent.sources.source.command = tail -F /path/logs/*_details.log
httpagent.sources.source.restart = true
httpagent.sources.source.restartThrottle = 1000
httpagent.sources.source.logStdErr = true

how to take log file backup automatically

How to take automatically backup of a log file(.txt) when it's size reached a threshold level, say 5MB. The backup file name should be like (log_file_name)_(system_date) and original log file should be cleaned(0 KB).
Please help. Thanks in advance.
Check your log file size using lenght().Then check if its bigger then 5mb call extendLogFile() func.
This is c# code u can easly convert to java
Size check:
if (size > 400 * 100 * 100)
{
extendLogFile(Path);
}
Copy old log file in archive directory and create new log file:
private static void extendLogFile(string lPath)
{
string name = lPath.Substring(0, lPath.LastIndexOf("."));
string UniquName = GenerateUniqueNameUsingDate(); // create a unique name for old log files like '12-04-2013-12-43-00'
string ArchivePath = System.IO.Path.GetDirectoryName(lPath) + "\\Archive";
if (!string.IsNullOrEmpty(ArchivePath) && !System.IO.Directory.Exists(ArchivePath))
{
System.IO.Directory.CreateDirectory(ArchivePath);
}
string newName = ArcivePath + "\\" + UniquName;
if (!File.Exists(newName))
{
File.Copy(lPath, newName + ".txt");
using (FileStream stream = new FileStream(lPath, FileMode.Create))
using (TextWriter writer = new StreamWriter(stream))
{
writer.WriteLine("");
}
}
}

edit any file which is wrapped in the jar file

I want to implement Following stuff with my java code in eclipse.
i need to edit the .dict file which is in directory of jar file.
my directory structure is like
C:\Users\bhavik.kama\Desktop\Sphinx\sphinx4-1.0beta6-bin\sphinx4-1.0beta6\modified_jar_dict\*WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar*\dict\**cmudict04.dict**
Text with bold character is my text file name which i want to edit
and text with italic foramt is my .jar file
now how can i edit this cmudict04.dict file which is reside in WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar\dict\ directory on runtime with java application.
and i want the jar file with the updated file i have edited.
please can u provide me any help?
thnank you in advance.
I would recommend to use java.util.zip.Using these classes you can read and write the files inside the archive .But modifying the contents is not guaranteed because it may be cached.
Sample tutorial
http://www.javaworld.com/community/node/8362
You can't edit files that are contained in a Jar file and have it saved in the Jar file ... Without, extracting the file first, updating it and creating a new Jar by copying the contents of the old one over to the new one, deleting the old one and renaming the new one in its place...
My suggestion is find a better solution
I had succeded to edit jar file and wrap it back as it is...with the following code
public void run() throws IOException
{
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
// JarOutputStream target = new JarOutputStream(new FileOutputStream("E:\\hiren1\\WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar"), manifest);
// add(new File("E:\\hiren1\\WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz/"), target);
JarOutputStream target = new JarOutputStream(new FileOutputStream("C:\\Users\\bhavik.kama\\Desktop\\Sphinx\\sphinx4-1.0beta6-bin\\sphinx4-1.0beta6\\modified_jar_dict\\WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar"), manifest);
add(new File("C:\\Users\\bhavik.kama\\Desktop\\Sphinx\\sphinx4-1.0beta6-bin\\sphinx4-1.0beta6\\modified_jar_dict\\WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz/"), target);
target.close();
}
private void add(File source, JarOutputStream target) throws IOException
{
BufferedInputStream in = null;
try
{
if (source.isDirectory())
{
//String name = source.getPath().replace("\\", "/");
if(isFirst)
{
firstDir = source.getParent() + "\\";
isFirst = false;
}
String name = source.getPath();
name = name.replace(firstDir,"");
if (!name.isEmpty())
{
if (!name.endsWith("/"))
name += "/";
JarEntry entry = new JarEntry(name);
entry.setTime(source.lastModified());
target.putNextEntry(entry);
target.closeEntry();
}
for (File nestedFile: source.listFiles())
add(nestedFile, target);
return;
}
String name = source.getPath();
name = name.replace(firstDir,"").replace("\\", "/");
//JarEntry entry = new JarEntry(source.getPath().replace("\\", "/"));
JarEntry entry = new JarEntry(name);
//JarEntry entry = new JarEntry(source.getName());
entry.setTime(source.lastModified());
target.putNextEntry(entry);
in = new BufferedInputStream(new FileInputStream(source));
byte[] buffer = new byte[1024];
while (true)
{
int count = in.read(buffer);
if (count == -1)
break;
target.write(buffer, 0, count);
}
target.closeEntry();
}
finally
{
if (in != null)
in.close();
}
}