PHP code example of lfphp / logger

1. Go to this page and download the library: Download lfphp/logger library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lfphp / logger example snippets



use LFPhp\Logger\Logger;

//Business start ...
class MyClass {
private $logger;
	public function __construct(){
$this->logger = Logger::instance(__CLASS__);

//Register specific log object event processing
$this->logger->register(function($messages){
echo "Log from internal";
var_dump($messages);
echo PHP_EOL;
});

$this->logger->debug('class construct.'); //Logging in the object
	}

	public function foo() {
		$msg = "I'm calling foo()";
		$this->logger->info($msg); //Logging in the object
		return $msg;
	}

	public function castError(){
		$this->logger->warning('warning, error happens'); //Logging in the object
	}

	public function __destruct(){
		$this->logger->warning('class destruct.'); //Logging in the object
	}
}

//Global logging
Logger::debug('Global logging start...');

$obj = new MyClass();
Logger::info('Object created', $obj);

$obj->foo();
$obj->castError();
unset($obj);

//Global logging
Logger::warning('Object destructed');


use LFPhp\Logger\LoggerLevel;
use LFPhp\Logger\Output\ConsoleOutput;
use LFPhp\Logger\Output\FileOutput;
use LFPhp\Logger\Logger;
use LFPhp\Logger\test\MyClass;

l to INFO to the file
Logger::registerGlobal(new FileOutput(__DIR__.'/log/Lite.debug.log'), LoggerLevel::INFO);

//Record all log information of the registration ID Curl::class (generally the class name is used as the registration ID) to the file
Logger::registerGlobal(new FileOutput(__DIR__.'/log/Lite.curl.log'), LoggerLevel::DEBUG, MyClass::class);

//Only when a WARNING level log event occurs, log all information with a level greater than or equal to INFO to the file
Logger::registerWhileGlobal(LoggerLevel::WARNING, new FileOutput(__DIR__.'/log/Lite.error.log'), LoggerLevel::INFO);

//Process the information yourself
Logger::registerGlobal(function($messages, $level){
	var_dump($messages);
	//Execute processing logic
}, LoggerLevel::INFO);

//Start normal business