rcezea / httplogger
Lightweight and flexible logging library for PHP applications. It enables developers to send log data to a remote server and log to local files.
Installs: 3
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/rcezea/httplogger
Requires
- ext-curl: *
- tracy/tracy: ^2.10
README
HTTPLogger Class Documentation
Class Name: HttpLogger
Namespace: Rcezea\HttpLogger
Implements: Tracy\ILogger
Overview
The HttpLogger class implements the ILogger interface to send error logs to a server endpoint while providing optional local file logging. It supports both web and mobile platforms and enables environment-based logging modes (Development or Production).
Features
- Server-Side Logging: Sends logs to a configurable server endpoint.
- Optional File Logging: Logs success and failed attempts to local files for debugging.
- API Authentication: Supports optional credentials (
appIdandsecretKey) for secure logging. - Platform-Specific Logging: Configurable for
webormobile. - Environment Modes: Differentiates between Development and Production modes.
Static Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
$endpoint |
string |
'http://127.0.0.1:5000/errorhandler' |
Server endpoint URL for logs. |
$mode |
bool |
Debugger::Development |
Logging mode: Development or Production. |
$platform |
string |
'web' |
Specifies platform: web or mobile. |
Constructor
Method Signature:
public function __construct( ?string $appId = NULL, ?string $secretKey = NULL, bool $logToFile = FALSE, ?string $logDir = NULL )
Parameters:
$appId(string|null): Optional API authentication ID.$secretKey(string|null): Optional API secret key.$logToFile(bool): Enables or disables file logging.$logDir(string|null): Directory path for storing log files. Defaults to../logsif not provided.
Throws:
\RuntimeExceptionif the log directory cannot be created.
Static Methods
-
setEndpoint(string $url): void
Sets the logging server endpoint.
Throws:\InvalidArgumentExceptionif the URL is invalid. -
setMode(int $mode): void
Sets the environment mode:Debugger::DevelopmentorDebugger::Production.
Throws:\InvalidArgumentExceptionfor invalid modes. -
setPlatform(string $platform): void
Sets the platform type (webormobile).
Throws:\InvalidArgumentExceptionfor invalid platforms.
Instance Methods
-
log(mixed $value, string $level = ILogger::INFO): void
Logs messages or exceptions to the server and optionally to local files.$value(mixed): The message or exception to log.$level(string): Log level, e.g.,ILogger::INFO.
Behavior:
- Sends logs to the configured endpoint using
cURL. - Includes optional API credentials.
- Determines the mode (
developmentorproduction) based on the static property. - Writes to local files if file logging is enabled.
-
sendLogToServer(array $data): void
Sends the prepared log data to the server endpoint usingcURL. -
logToFile(string $filePath, array $data, string $response = ''): void
Writes logs to the specified file path.
Logging to Files
If $logToFile is enabled during object instantiation, logs will be written to:
- Success Logs:
success_log.txt - Failed Logs:
failed_log.txt
Format of File Logs:
[2024-06-17 10:45:00]
LEVEL: info
Message: Sample log message
Stack: N/A
Platform: web
Environment: development
-----------------------------------
For failed attempts, curl_error and server responses will be included.
Usage Example
require_once dirname(__DIR__) . '/vendor/autoload.php'; // map to exact autoload location use Rcezea\HttpLogger\HttpLogger; use Tracy\Debugger; use Tracy\ILogger // Initialize Logger $logger = new HttpLogger( appId: 'myAppId', secretKey: 'mySecretKey', logToFile: true ); // Set optional configurations HttpLogger::setEndpoint('https://api.example.com/logs'); HttpLogger::setPlatform('mobile'); HttpLogger::setMode(Debugger::Production); // Log an exception try { throw new \Exception('Something went wrong!'); } catch (\Throwable $e) { $logger->log($e, ILogger::ERROR); } // Log a message $logger->log('This is an informational message.', ILogger::INFO);
Requirements
- PHP 8.0+
- Composer dependencies:
tracy/tracy
Changelog
- Added platform configuration (
setPlatform). - Enhanced file logging to include success and failure logs.
- Integrated API authentication.
License
This package is licensed under the MIT License.
Contributing
Contributions are welcome! Feel free to submit pull requests or open issues for improvements or bug fixes.
Author
- Richard E - Github: rcezea
Support
For questions, issues, or support, please contact rclancing@gmail.com.