Namespace: TrishulApi\Core\Log
Author: Shyam Dubey
Since: v1.0.0
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.
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.');
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.
Parameter | Type | Description |
---|---|---|
$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.
Parameter | Type | Description |
---|---|---|
$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()
.
Parameter | Type | Description |
---|---|---|
$message | string | The message to log. |
public function info($message): void
Logs a message with INFO level.
Parameter | Type | Description |
---|---|---|
$message | string | The message to log. |
public function warn($message): void
Logs a message with WARN level.
Parameter | Type | Description |
---|---|---|
$message | string | The warning message to log. |
public function error($message): void
Logs a message with ERROR level.
Parameter | Type | Description |
---|---|---|
$message | string | The error message to log. |
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!
set_log_dir()
, logs are written to daily log files named YYYY-MM-DD.log
in that directory.get_logger()
to obtain a logger for your class.TrishulApi\Core\Log\LoggerInterface
TrishulApi\Core\Log\LogPattern