By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,851 Members | 1,410 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,851 IT Pros & Developers. It's quick & easy.

How to take backup of a running log file and search for a pattern in that log file

P: 1
Hi All,
I have different subsystems in my application, if one is down it will through some error message in log file. I have a collection of error messages in another file and I want to check each message against a log file.

my application is running on tomcat and log file will continuously appended by the server even there are no requests for server. My requirement is when I hit the server with a request, I want to capture the log of my request only into another file. Once the log file is created, I want to search for a particular pattern in that file. If pattern find I want to take only boolean value of search. Can some body help on this.

I tried by nullifying the log file before my request hits the server in my script, then it is truncating the all the lines before my request hits the server and adding garbage value for this. Then grep is treating that file as binary file and coming out of this. given below are the
perl stmts I used in my script. Some one please help on this.

Expand|Select|Wrap|Line Numbers
  1. $dest="APG.log";
  2. $source="/home/work/APGlogs/APG.log";
  3. system("cp /dev/null /home/work/APGlogs/APG.log");
  4. copy($source, $dest)
  5. my $ret = grep ($input[1],$dest);
Feb 20 '08 #1
Share this Question
Share on Google+
1 Reply

Expert 100+
P: 410
I could not understand your requirement completely. Is that you want to open the log file as it is being created/ getting appended with log messages and search for the pattern?
Anyway, I understood that once the log file is created, you want to search for a particular pattern in that file and take Boolean value of search. There are few issues in the way you have used grep:

Expand|Select|Wrap|Line Numbers
  1. my $ret = grep ($input[1],$dest);
Firstly, pattern should be included within //. Next, you have passed $dest (which is filename) as second argument. So, this will only try to find the pattern in filename. If you want to search inside file, use this:

Expand|Select|Wrap|Line Numbers
  1. open(IN,$dest) or die "cannot open file:$!";
  2. @contents=<IN>;
  3. ## the following line will return the number of occurences of pattern in the file
  4. my $ret = grep (/$input[1]/,@contents);
  5. ### you can also use the following way to return 1 or 0
  6. # $res=0;
  7. # $res=1 if( grep (/$input[1]/,@contents));
Feb 20 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.