承接 laswitchtech/php-logger 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

laswitchtech/php-logger

最新稳定版本:v1.2.11

Composer 安装命令:

composer require laswitchtech/php-logger

包简介

Easy to use logging library for PHP

README 文档

README

GitHub repo logo

phpLogger - [DEPRECATED] - Use coreLogger instead

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;
    }
  }
}

统计信息

  • 总下载量: 319
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 8
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-only
  • 更新时间: 2023-03-08

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固