laswitchtech/php-logger

Easy to use logging library for PHP

v1.2.9 2024-05-02 19:30 UTC

README

GitHub repo logo

phpLogger

License GitHub repo size GitHub top language Version

Description

The phpLogger class is a PHP package for logging messages to files. It supports logging at different levels of severity, including DEBUG, INFO, SUCCESS, WARNING, and ERROR. It also provides support for rotating log files, which can help manage file sizes and ensure that logs don't become too large.

Features

  • Supports logging at different severity levels: DEBUG, INFO, SUCCESS, WARNING, and ERROR.
  • Rotates log files to manage file sizes.
  • Provides the ability to add, set, and list log files.
  • Supports logging to multiple files.
  • Supports logging ip addresses.

Why you might need it?

Logging is an essential part of debugging and monitoring software applications. It can help developers identify issues and bugs, as well as provide insight into how users are interacting with the software. The phpLogger class provides a simple and flexible way to log messages to files in PHP applications. It can help developers quickly set up logging functionality and manage log files, making it an essential tool for any PHP project.

Can I use this?

Sure!

License

This software is distributed under the GNU General Public License v3.0 license. Please read LICENSE for information on the software availability and distribution.

Requirements

  • PHP >= 7.3

Security

Please disclose any vulnerabilities found responsibly – report security issues to the maintainers privately.

Installation

Using Composer:

composer require laswitchtech/php-logger

How do I use it?

Usage

Initiate phpLogger

To use phpLogger, simply include the phpLogger.php file and create a new instance of the phpLogger class. By default, it will create a log file named "default.log" in the same directory as the phpLogger.php file.

//Import phpLogger class into the global namespace
//These must be at the top of your script, not inside a function
use LaswitchTech\phpLogger\phpLogger;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Initiate phpLogger
$phpLogger = new phpLogger();

Logging Messages

To log a message, use the log method of the phpLogger class. By default, it will log the message at the INFO level to the "default.log" file.

$phpLogger->log("This is a log message");

You can also specify a log level and log file name:

$phpLogger->log("This is a debug message", phpLogger::LEVEL_DEBUG, "debug");

Customizing Log Files

You can add, switch between, and rotate log files using the add, set, and rotate methods of the phpLogger class.

// Add a new log file named "error.log"
$phpLogger->add("error", "error.log");

// Switch to the "error" log file
$phpLogger->set("error");

// Rotate the current log file
$phpLogger->rotate();

List Log Files

You can also list all of the log files using the list method:

$files = $phpLogger->list();
print_r($files);

Customizing Log Message Format

You can customize the format of the logged message by subclassing phpLogger and overriding the log method. For example, the following code logs the message with the date and time, log level, calling function or method, file name, and line number:

class MyLogger extends phpLogger {
  public function log($message, $level = self::LEVEL_INFO, $logName = null){
    $trace = debug_backtrace();
    $caller = isset($trace[1]) ? $trace[1] : $trace[0];
    $file = isset($caller['file']) ? $caller['file'] : '';
    $line = isset($caller['line']) ? $caller['line'] : '';
    $class = isset($caller['class']) && count($trace) > 1 ? $caller['class'] : '';
    $function = isset($caller['function']) && count($trace) > 1 ? $caller['function'] : '';

    $classTrace = '';
    if($class != ''){
      $classTrace .= $class . '::';
    }
    if($function != ''){
      $classTrace .= $function;
    }
    if($classTrace != ''){
      $classTrace = " [$classTrace]";
    }

    $timestamp = date("Y-m-d H:i:s");
    $logLine = "[$timestamp] [$level]$classTrace ($file:$line) $message"

    if(is_file($logFile)){
      $today = new DateTime();
      $logDate = new DateTime();
      $logDate->setTimestamp(filemtime($logFile));
      if($today->format("Y-m-d") > $logDate->format("Y-m-d")){
        $fileName = $logFile . '.' . strtotime($logDate->format("Y-m-d"));
        rename($logFile, $fileName);
      }
    }

    file_put_contents($logFile, $logLine, FILE_APPEND);

    if(defined('STDIN')){
      echo $logLine;
    }
  }
}