PHP code example of samayo / autolog

1. Go to this page and download the library: Download samayo/autolog 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/ */

    

samayo / autolog example snippets



$log = new Autolog\Logger(["email" => "[email protected]"]);

if($comment){
   $log->log("$user just commented \n $comment", $log::INFO, $log::EMAIL); 	
}

/**
 * $msg (onal) the message type: error, info, notification..
 * $handler (optional) where to send it (db, email, file log)
 * $verbosity (optional) log as simple or verbose info
 */
log($msg, $type, $handler, $verbosity);

$type::INFO; // for simple tasks
$type::ERROR; // for errors ex: 404 .. 
$type::ALERT; // for fatal errors or suspicious activity

$handler::EMAIL; // send to email
$handler::FILE; // write to file
$handler::DATABASE; // insert to database 
$handler::SMS; // send to sms (not yet implemented)

$verbosity::SIMPLE; // send simplified log
$verbosity::VERBOSE; // send every log information

// the below will log an error, in verbose format and mail it
$log = new Autolog\Logger(["email" => "[email protected]"]);
$log->log($msg, $log::ERROR, $log::EMAIL, $log::VERBOSE);
 
$log = new Autolog\Logger; 
$log["email"] = "[email protected]"; // add email

// or add your email list this: 
$log = new Autolog\Logger(["email" => "[email protected]"]); 

// then log it!
if($something){
   $log->log("something"); // email 'something'
}

$log = new Autolog\Logger(["error.log" => __DIR__ . "/mylogs.txt"]); 
$log->log("ERROR: $error", $log::INFO, $log::FILE); // don't forget $log::FILE

$log = new Autolog\Logger;
$log->pdo(new \PDO(
   // your pdo host, db, pass here
)); 
$log->log("simple log", $log::ERROR, $log::DATABASE);

(new \Autolog\Logger)
   ->pdo(new PDO(/**/))->log("user: $user modified his/her profile", $log::INFO, $log::DATABASE); 
 
$logger = Autolog\Logger(["email" => "[email protected]"]); 

// mail all thrown exceptions
set_exception_handler(function($e) use($logger){
   $logger->log($e, $log::ERROR, $log::EMAIL);
}); 

// mail all errors
set_error_handler(function($no, $str, $file, $line) use ($logger){
   $logger->log("Your site has error: $str in file $file at line $line", $log::ERROR, $log::EMAIL);
})

// always watch new errors that appear in (nginx, php) log files
$log->watch(true);

// log_mailer.php
([
  "nginx.log"  => "/var/log/nginx/error.log",
  "php-fpm.log"  => "/var/log/php-fpm/error.log",
  "mariadb.log" => "/var/log/mariadb/mariadb.log",
  "access.log"  => "access.txt",
  "email"  => "[email protected]"
]))->watch(true);