Versionen im Vergleich

Schlüssel

  • Diese Zeile wurde hinzugefügt.
  • Diese Zeile wurde entfernt.
  • Formatierung wurde geändert.
Kommentar: V1.2.5 improvements

...

Codeblock
languageperl
titleSimple logging
use LoxBerry::Log;
 
# Create a logging object
my $log = LoxBerry::Log->new ( name => 'daemon' );

# We start the log. It will print and store some metadata like time, version numbers, and the string as log title 
LOGSTART "LoxBerryDaemon daemon startsstarted";
 
# NowThis weare reallythe loglogging shortcuts, ascending from lowest to highest:
LOGDEB "This is debugging";  				# Loglevel 7
LOGINF "Infos in your log";					# Loglevel 6
LOGOK "Everything is OK";					# Loglevel 5
LOGWARN "Hmmm, seems to be a Warning";		# Loglevel 4
LOGERR "Error, that's not good";			# Loglevel 3
LOGCRIT "Critical, no fun";					# Loglevel 2
LOGALERT
"Alert,# ring ring!";				# Loglevel 1
LOGEMERGE "Emergency, for really really hard issues";	# Loglevel 0
LOGTITLE "LoxBerry daemon 123"; # Changes the log title of LOGSTART (V1.2.5 and above) 
You may want to change the log title to be more specific, e.g. after reading your config file
LOGTITLE "Daemon for backup started";


# To indicate, that your script has finished (with errors or not), close the logging session
LOGEND "Operation finished sucessfully.";

...

For this example, a logfile is created in yours your plugin log folder. It's name is the timestamp and name 'daemon' (e.g. "20171222_093157_235_daemon.log) - you can create different logs for different tasks - therefore provide different names.

Usually you don't need all seven severities. For simple logging you possibly be ok with LOGINF, LOGERR and LOGDEB. It is handy common practice to use a LOGCRIT if your script needs to terminate early because of an unrecoverable error. Currently do not use LOGALERT and LOGEMERGE, as this will possibly become some automatic (email) altering stuff. 

Are line feeds (\n) required?

...

You don't need to care about log levels at all! Simply use the severity keywords (LOGDEB, LOGINF, LOGOK and so on, as you need it. LogBerry::Log entries are filtered by the Plugin loglevel that is defined by the user at the Plugins system widget (where you install plugins). Therefore, you even don't need to provide your own loglevel setting in the plugin. Default log level after a plugin installation is 3 (Errors or more critical).

...

Codeblock
languageperl
$log->loglevel(5);

  But we advise you not to hardcode the loglevel. Let the user decide.

For your first logging steps, you can stop reading here, and start logging NOW. You always can test your logfiles in the Log Manager widget.

I want to pipe a system() output to the log

Nothing easier than that!

You always can request the logfile name to use outside of LoxBerry::Log:

Codeblock
languageperl
LOGINF "Current directory entries:";


my $logfile = $log->filename();
system("ls -l >> $logfile");

LOGOK "All files listed.";

From LoxBerry V1.2.5 the logging feature does not keep open the logfile. At any time you can pipe output to that filename.

For plugins on LoxBerry < V1.2.5 it is required to always close the file before every pipe:

Codeblock
languageperl
LOGINF "Current directory entries:";


my $logfile = $log->close();
system("ls -l >> $logfile");

$log->open();
LOGOK "All files listed.";

The "old" syntax (2st codeblock) is still supported in V1.2.5, no code needs to be changed.

I want to use a command that directly writes to a filehandle

Simply query the logfile filehandle:

Codeblock
languageperl
my $fh = $log->filehandle;

I need two log objects in one session

You are a logging king! Therefore, you can use multiple logfiles in one session. 

...

Codeblock
languageperl
titleSimple logging
use LoxBerry::Log;
 
# Create a logging object
my $logone = LoxBerry::Log->new ( name => 'daemon' );
$logone->LOGSTART "Daemon log started.";

# Not enough - create a second
my $logtwo = LoxBerry::Log->new (name => 'saruman' );
$logtwo->LOGSTART "Saruman log started.";
 
# Now we start to log, ascending from lowest to highest:
$logone->DEB "This is debugging";
$logtwo->INF "Infos in your log";
$logone->OK "Everything is OK";
$logtwo->WARN "Hmmm, seems to be a Warning";
$logone->ERR "Error, that's not good";
# Be aware, the object functions do not use the LOG prefix.
# But the easy logging keywords still work - with the first logfile.
# Goes to $log
LOGCRIT "Critical, no fun";
LOGALERT "Alert, ring ring!";
LOGEMERGE "Emergency, for really really hard issues";
 
# If you log in bulks, you can switch the default logfile:
$logtwo->default;
LOGOK "This now goes to $logtwo"

 

I want to pipe a system() output to the log

Nothing easier than that!

You always can request the logfile name to use outside of LoxBerry::Log:

Codeblock
languageperl
my $logfile = $log->close;
system("ls -l >> $logfile");
$log->open;

 

As Perl keeps the logfile open during a system() call, you need to close the file, and reopen it after your pipe.

$log->close will return the filename to use in your pipe.

I want to use a command that directly writes to a filehandle

Simply query the logfile filehandle:

Codeblock
languageperl
my $fh= $log->filehandle;

 

Never close the filehandle yourself because LoxBerry::Log then will fail and die badly.

How can I hand over the same log from script to script

...

Codeblock
languageperl
# Script 1
my $logfile = $log->close;
system("myscript2.pl $logfile");
 
# Script 2
# Do some CGI or GetOpt stuff to get the parameter
my $log = LoxBerry::Log->new ( name => "daemon", filename => $filename, append => 1 );
 

 

Providing the same name, the filename got from the first script, and the append setting, LoxBerry::Log will continue to log to the same log in your script number 2.


Starting with LoxBerry V1.2.5, there is an easier way to hand over a logfile:

Codeblock
languageperl
# Script 1
LOGINF "Preparing handover to script 2..."; 
my $dbkey = $log->dbkey;
system("myscript2.pl $dbkey");
 
# Script 2
# Do some CGI or GetOpt stuff to get the parameter
my $log = LoxBerry::Log->new ( dbkey => $dbkey );
LOGOK "Script 2 is now running in the same logfile";

The second script recovers all the options set in the first script. No LOGEND is needed in Script 1, and no LOGSTART is needed in Script 2. Script 2 is set to append=1 automatically, independent of the setting in script 1.

For this handover, it is not allowed to trigger LOGEND in script 1, as this would finalize the logging session.


I want also output to STDOUT or STDERR

...

Codeblock
languageperl
titleSimple logging
use LoxBerry::Log;
 
# Create a logging object
my $log = LoxBerry::Log->new ( name => 'daemon' );

# Enable STDERR
$log->stderr(1);
 
# Enable STDOUT
$log->stdout(1);
 
# Disable writing to the logfile
$log->nofile(1);

 

Default is:

  • writing to nofile = 0 (→ will write a logfile)
  • stderr is off
  • stdout is off
  •  = 0 (→ no stderr output)
  • stdout = 0 (→ no stdout output)

These three settings are independent to each other - none of the settings disables another setting. Example: strerr=1 and stdout=1 will print each line twice in the console.

This especially may be handy during plugin development, to get the errors directly to STDERR instead of checking new logfiles all the time.

You can set these settings also directly in the new constructor, so you can use LoxBerry::Log even if you don't need a logfile at all! ( stderr => 1, nofile => 1);

Do not use stdout=1 in web scripts, as the break the html and will lead to a server ERROR 500. 

Log Title (V1.2.5 and above)

...

Codeblock
languageperl
titleSimple logging
use LoxBerry::Log;
 
# Create a logging object
my $log = LoxBerry::Log->new ( name => 'daemon' );
LOGSTART "Daemon Processing";


# ... read something from the config file ...


LOGTITLE "Daemon Processing for $process";

 

The auto-raise function

Our recommendations:

...

Codeblock
languageperl
titleSimple logging
use LoxBerry::Log;
 
# Create a logging object
my $log = LoxBerry::Log->new ( name => 'cronjob', package => 'Update',  loglevel => 7);
 
# Now we start to log, ascending from lowest to highest:
LOGSTART "Update by cron job";
LOGDEB "This is debugging";
LOGINF "Infos in your log";
LOGOK "Everything is OK";
# that's equal....

...