Sponsored Content
Top Forums UNIX for Dummies Questions & Answers [Solved] How to Redirect Output To Log File? Post 302891040 by Ariean on Monday 3rd of March 2014 11:37:15 AM
Old 03-03-2014
Quote:
Originally Posted by RavinderSingh13
Hello,

If you want everything to be printed to a file, please use redirect >> command after each echo command.

Like for example as follows.

Code:
echo "STEP 1: Datafile Transmitted: "$2", DataFile Input Folder: "$1"" >> OUTPUT_file


Thanks,
R. Singh
I understand that redirection & appending, but instead of that can i do any modifications to the below line, so that even the echo commands after that would just print to the same log file.

exec > >(tee "/var/log/ScriptLogs/called_from_incrontab.log") 2>&1
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

redirect output to file?

Hi: I am currently working on a program which requires direct its ouput to a file here is an example ./proram arg_1 arg_2 when program ends all output will be arg_2 file Is that possible I am not a bad programmer, However I am stuck there. Can anyone give a hint? Thanks SW (1 Reply)
Discussion started by: slackware
1 Replies

2. UNIX for Dummies Questions & Answers

Redirect output to a file

Ahhhrrrggg I'm having a brain fart... I want to take the output of a command and redirect it to a file... This works.... $ man cp | cat >> copy_help but this doesn't keytool -help |cat >> keytool_help It just produces... these lines... more keytool_help ] ... ... (11 Replies)
Discussion started by: jimmyc
11 Replies

3. Shell Programming and Scripting

redirect output to log file

hello all, I'm invoking the program generate-report using backticks from my perl program and redirecting the output to the log file sge-stderr.log. But when i check the process using ps command it is spawing two processes where the below code is parent process and the program generate-report as... (2 Replies)
Discussion started by: kalyanraj
2 Replies

4. UNIX for Dummies Questions & Answers

redirect output to a file name

Hi all!! is possible to assign the output of some command to filename, i.e. grep_output.txt Otherwise, I want to open a new file which name is inside another, how can I do it? Thanks a lot! (7 Replies)
Discussion started by: csecnarf
7 Replies

5. UNIX for Dummies Questions & Answers

redirect output into the middle of a file

If I want to cat one file and have the output inserted into a specific place on another file, how is this done? I know how to append >> and to overwrite > but say I have a file with: File1: abc def ghi jkl And a File with: File2: mno pqr stu vwx And I want to place the... (5 Replies)
Discussion started by: glev2005
5 Replies

6. Shell Programming and Scripting

How to redirect output of ls to a file?

Hi All, I want to redirect only the file names to a new file from the ls -ltr directroy. how Can i do it. my ls -ltr output will be as below. -rwxr-xr-x 1 118 103 28295 Jul 26 2006 event.podl -rwxr-xr-x 1 118 103 28295 Jul 26 2006 xyz.podl I want my new file... (6 Replies)
Discussion started by: girish.raos
6 Replies

7. Shell Programming and Scripting

Redirect the output in a file and on screen

I am trying to get following result from the scipt I have. First time it generates the o/p in correct format. However if I run it again it appends to the existing file. I would like to see o/p on screen as well as save it in file. Everytime it should create new file. ## I/P file 0174 0175... (3 Replies)
Discussion started by: dynamax
3 Replies

8. Shell Programming and Scripting

script to mail monitoring output if required or redirect output to log file

Below script perfectly works, giving below mail output. BUT, I want to make the script mail only if there are any D-Defined/T-Transition/B-Broken State WPARs and also to copy the output generated during monitoring to a temporary log file, which gets cleaned up every week. Need suggestions. ... (4 Replies)
Discussion started by: aix_admin_007
4 Replies

9. Shell Programming and Scripting

Redirect script output to a file and mail the output

Hi Guys, I want to redirect the output of 3 scripts to a file and then mail the output of those three scripts. I used below but it is not working: OFILE=/home/home1/report1 echo "report1 details" > $OFILE =/home/home1/1.sh > $OFILE echo... (7 Replies)
Discussion started by: Vivekit82
7 Replies

10. Shell Programming and Scripting

Redirect the output of the telnet to a file

Hi, I am using cygwin. Below is my script that reads all ip ports for iplist.txt and telnets to it. ( file="iplist.txt" while read line do echo $line echo $(telnet $line) done <"$file" ) > output2.txt ~ while the output2.txt gets the first echo but does not show the second... (2 Replies)
Discussion started by: mohtashims
2 Replies
Log::Handler::Examples(3pm)				User Contributed Perl Documentation			       Log::Handler::Examples(3pm)

NAME
Log::Handler::Examples - Examples. CREATE LOGGER
Quite simple use Log::Handler; my $log = Log::Handler->new(); $log->add( screen => \%options ); Create a application wide logger my $log = Log::Handler->create_logger("myapp"); $log->add( screen => \%options ); Once created you can use the application logger in all modules of your project: package MyApp; use Log::Handler; my $log = Log::Handler->create_logger("myapp"); $log->add( screen => \%options ); package MyApp::Admin; use Log::Handler; my $log = Log::Handler->get_logger("myapp"); $log->info("message"); ADD OUTPUTS
use Log::Handler; my $log = Log::Handler->new(); $log->add( dbi => \%options ); $log->add( email => \%options ); $log->add( file => \%options ); $log->add( forward => \%options ); $log->add( screen => \%options ); $log->add( socket => \%options ); This is the same like $log->add( "Log::Handler::Output::DBI" => \%options ); $log->add( "Log::Handler::Output::Email" => \%options ); $log->add( "Log::Handler::Output::File" => \%options ); $log->add( "Log::Handler::Output::Forward" => \%options ); $log->add( "Log::Handler::Output::Screen" => \%options ); $log->add( "Log::Handler::Output::Socket" => \%options ); RELOAD THE LOGGER
Quite simple use Log::Handler; my $log = Log::Handler->new(); $log->config(config => "logger.conf"); $log->reload(config => "logger.conf"); Reload on HUP use Log::Handler; my $log = Log::Handler->new(); $log->config(config => "logger.conf"); $SIG{HUP} = sub { unless ($log->reload(config => "logger.conf")) { warn "unable to reload configuration"; warn $log->errstr; } }; Validate first It's possible to make a configuration check before you reload: $log->validate(config => "logger.conf") or warn $log->errstr; LOG VIA DBI
use Log::Handler; my $log = Log::Handler->new(); $log->add( dbi => { database => "database", driver => "mysql", host => "127.0.0.1", port => 3306, user => "user", password => "password", table => "messages", columns => [ qw/level ctime cdate pid hostname caller progname mtime message/ ], values => [ qw/%level %time %date %pid %hostname %caller %progname %mtime %message/ ], maxlevel => "error", minlevel => "emergency" newline => 0, message_pattern => "%L %T %D %P %H %C %S %t %m", } ); $log->error("log an error"); Or with "dbname" $log->add( dbi => { dbname => "database", driver => "Pg", host => "127.0.0.1", port => 5432, user => "user", password => "password", table => "messages", columns => [ qw/level ctime cdate pid hostname caller progname mtime message/ ], values => [ qw/%level %time %date %pid %hostname %caller %progname %mtime %message/ ], maxlevel => "error", minlevel => "emergency" newline => 0, message_pattern => "%L %T %D %P %H %C %S %t %m", } ); Or with "data_source" $log->add( dbi => { data_source => "dbi:SQLite:dbname=database.sqlite", table => "messages", columns => [ qw/level ctime cdate pid hostname caller progname mtime message/ ], values => [ qw/%level %time %date %pid %hostname %caller %progname %mtime %message/ ], maxlevel => "error", minlevel => "emergency" newline => 0, message_pattern => "%L %T %D %P %H %C %S %t %m", } ); LOG VIA EMAIL
use Log::Handler; my $log = Log::Handler->new(); $log->add( email => { host => "mx.bar.example", hello => "EHLO my.domain.example", timeout => 30, from => "bar@foo.example", to => "foo@bar.example", subject => "your subject", buffer => 0, maxlevel => "emergency", minlevel => "emergency", message_pattern => '%L', } ); $log->emergency("log an emergency issue"); LOG VIA SENDMAIL
use Log::Handler; my $log = Log::Handler->new(); $log->add( sendmail => { from => "bar@foo.example", to => "foo@bar.example", subject => "your subject", maxlevel => "error", minlevel => "error", message_pattern => '%L', } ); $log->emergency("message"); LOG VIA FILE
use Log::Handler; my $log = Log::Handler->new(); $log->add( file => { filename => "file1.log", maxlevel => 7, minlevel => 0 } ); $log->error("log an error"); LOG VIA FORWARD
use Log::Handler; my $log = Log::Handler->new(); $log->add( forward => { forward_to => &my_func, message_pattern => [ qw/%L %T %P %H %C %S %t/ ], message_layout => "%m", maxlevel => "info", } ); $log->info("log a information"); sub my_func { my $params = shift; print Dumper($params); } LOG VIA SCREEN
use Log::Handler; my $log = Log::Handler->new(); $log->add( screen => { log_to => "STDERR", maxlevel => "info", } ); $log->info("log to the screen"); LOG VIA SOCKET
use Log::Handler; my $log = Log::Handler->new(); $log->add( socket => { peeraddr => "127.0.0.1", peerport => 44444, maxlevel => "info", die_on_errors => 0, } ); while ( 1 ) { $log->info("test") or warn "unable to send message: ", $log->errstr; sleep 1; } SIMPLE SOCKET SERVER (TCP) use strict; use warnings; use IO::Socket::INET; use Log::Handler::Output::File; my $sock = IO::Socket::INET->new( LocalAddr => "127.0.0.1", LocalPort => 44444, Listen => 2, ) or die $!; my $file = Log::Handler::Output::File->new( filename => "file.log", fileopen => 1, reopen => 1, ); while ( 1 ) { $file->log(message => "waiting for next connection "); while (my $request = $sock->accept) { my $ipaddr = sprintf("%-15s", $request->peerhost); while (my $message = <$request>) { $file->log(message => "$ipaddr - $message"); } } } DIFFERENT OUTPUTS
use Log::Handler; my $log = Log::Handler->new(); $log->add( file => { filename => "common.log", maxlevel => 6, minlevel => 5, } ); $log->add( file => { filename => "error.log", maxlevel => 4, minlevel => 0, } ); $log->add( email => { host => "mx.bar.example", hello => "EHLO my.domain.example", timeout => 120, from => "bar@foo.example", to => "foo@bar.example", subject => "your subject", buffer => 0, maxlevel => 0, } ); # log to common.log $log->info("this is a info message"); # log to error.log $log->warning("this is a warning"); # log to error.log and to foo@bar.example $log->emergency("this is a emergency message"); FILTER MESSAGES
my $log = Log::Handler->new(); $log->add( screen => { maxlevel => 6, filter_message => { match1 => "foo", match2 => "bar", match3 => "baz", condition => "(match1 && match2) && !match3" } } ); $log->info("foo"); $log->info("foo bar"); $log->info("foo baz"); FILTER CALLER This example shows you how it's possilbe to debug messages only from a special namespace. my $log = Log::Handler->new(); $log->add( file => { filename => "file1.log", maxlevel => "warning", } ); $log->add( screen => { maxlevel => "debug", message_layout => "message from %p - %m", filter_caller => qr/^Foo::Barz/, } ); $log->warning("a warning here"); package Foo::Bar; $log->info("an info here"); 1; ANOTHER FILTER filter_message => "as string" filter_message => qr/as regexp/ filter_message => sub { shift->{message} =~ /as code ref/ } # or with conditions filter_message => { match1 => "as string", match2 => qr/as regexp/, condition => "match1 || match2", } filter_caller => "as string" filter_caller => qr/as regexp/ CONFIG
Examples: my $log = Log::Handler->new( config => "logger.conf" ); # or $log->add( config => "logger.conf" ); # or $log->config( config => "logger.conf" ); Example with Config::General. Script: use Log::Handler; my $log = Log::Handler->new(); $log->config( config => "logger.conf" ); Config (logger.conf): <file> alias = common filename = example.log maxlevel = info minlevel = warn </file> <file> alias = error filename = example-error.log maxlevel = warn minlevel = emergency </file> <file> alias = debug filename = example-debug.log maxlevel = debug minlevel = debug </file> <screen> log_to = STDERR dump = 1 maxlevel = debug minlevel = debug </screen> CHECK FOR ACTIVE LEVELS
It can be very useful if you want to check if a level is active. use Log::Handler; use Data::Dumper; my $log = Log::Handler->new(); $log->add( file => { filename => "file1.log", maxlevel => 4, } ); my %hash = (foo => 1, bar => 2); Now you want to dump the hash, but not in any case. if ( $log->is_debug ) { my $dump = Dumper(\%hash); $log->debug($dump); } This would dump the hash only if the level debug is active. AUTHOR
Jonny Schulz <jschulz.cpan(at)bloonix.de>. perl v5.14.2 2012-11-21 Log::Handler::Examples(3pm)
All times are GMT -4. The time now is 03:56 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy