TrishulApi\Core\Log\LoggerFactory

Namespace: TrishulApi\Core\Log
Author: Shyam Dubey
Since: v1.0.0

Overview

The LoggerFactory class provides a simple and flexible logging mechanism for your application. It supports logging messages of different severity levels (INFO, WARN, ERROR) to a specified directory or to the default PHP error log. Each log entry includes a timestamp, log level, class name, and message, making it easy to trace and debug application events.

Usage Example


use TrishulApi\Core\Log\LoggerFactory;

// Set the log directory (optional, defaults to PHP error log)
LoggerFactory::set_log_dir(__DIR__ . '/logs');

// Get a logger instance for your class
$logger = LoggerFactory::get_logger('MyClass');

// Log messages
$logger->info('This is an info message.');
$logger->warn('This is a warning.');
$logger->error('This is an error.');
$logger->log('This is a generic log message.');
        

Methods

public static function set_log_dir($dir): void

Sets the directory where log files will be stored. If not set, logs are written to the default PHP error log.

ParameterTypeDescription
$dir string Path to the log directory.
public static function get_logger($className): LoggerFactory

Returns a logger instance for the specified class. Use this to log messages with the class context.

ParameterTypeDescription
$className string Name of the class for which logging is performed.

Returns: LoggerFactory instance

public function log($message): void

Logs a message with INFO level. Equivalent to info().

ParameterTypeDescription
$message string The message to log.
public function info($message): void

Logs a message with INFO level.

ParameterTypeDescription
$message string The message to log.
public function warn($message): void

Logs a message with WARN level.

ParameterTypeDescription
$message string The warning message to log.
public function error($message): void

Logs a message with ERROR level.

ParameterTypeDescription
$message string The error message to log.

Log Format

Each log entry is formatted as follows:


YYYY-MM-DD hh:mm:ss | LEVEL | ClassName.php | Message
        

Example:


2025-06-04 03:15:22 | ERROR | MyClass.php | Something went wrong!
        

Log Storage

Notes

Related Classes

Changelog