Skip to content

A very simple logger for PHP. No magic configuration required. Just the basic features you need most.

License

Notifications You must be signed in to change notification settings

thiagodp/logger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Logger

A very simple logger for PHP. No magic configuration required. Just the basic features you need most.

Provided interfaces and classes:

Available log methods:

  • bool debug( string $message, Exception $e = null, array $context = array() );
  • bool info( string $message, Exception $e = null, array $context = array() );
  • bool warn( string $message, Exception $e = null, array $context = array() );
  • bool error( string $message, Exception $e = null, array $context = array() );
  • bool log( string $logType, string $message, Exception $e = null, array $context = array() );

Installation

composer require phputil/logger

Example 1

<?php
require_once 'vendor/autoload.php'; // composer

use phputil\TextFileLogger;

// It is recommended to set the DateTimeZone when using TextFileLogger.
$logger = new TextFileLogger( 'log.txt', false, new \DateTimeZone( 'America/Sao_Paulo' ) );

$logger->info( 'Something will happen' );
$logger->debug( 'Something happened.' );
$logger->warn( 'Wait!' );
$logger->error( 'Ouch.' );

$logger->log( Logger::DEBUG, "That's awesome!" );
?>

Example 2

<?php
require_once 'vendor/autoload.php'; // composer

use phputil\Logger;
use phputil\TextFileLogger;
use phputil\FakeLogger;

$inDebugMode = true;

$logger = $inDebugMode
	? new TextFileLogger( 'log.txt', false, new \DateTimeZone( 'America/Sao_Paulo' ) )
	: new FakeLogger();

$logger->info( 'Something will happen' );
try {
	throw new \Exception( 'Hummm... something bad happened.' );
} catch ( \Exception $e ) {
	// Logs message and trace
	$logger->error( 'Ouch, I did not expect that!', $e );
}

$logger->log( Logger::DEBUG, "That's awesome!" );
?>

Example 3

<?php
require_once 'vendor/autoload.php'; // composer

use phputil\EchoLogger;

$logger = new EchoLogger();
$logger->info( 'It can log to the console too!' );
?>

About

A very simple logger for PHP. No magic configuration required. Just the basic features you need most.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages