initial commit

This commit is contained in:
Brad Cimbura
2024-02-07 13:55:00 -06:00
parent 5e7acf2abd
commit 89d2e6a86d
4 changed files with 232 additions and 0 deletions

41
README.md Normal file
View File

@@ -0,0 +1,41 @@
# Logutil
Logutil is a logging utility for PHP.
## Getting Started
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
### Prerequisites
You need to have PHP installed on your machine. Also, you need to install the dependencies of the project using Composer.
## Installation
You can install LogUtil via Composer. Run the following command in your project directory:
```sh
composer require hpz937/logutil
```
### Usage
You can use the Log class from the Hpz937\Logutil namespace to log messages. Here's an example:
```php
<?php
use Hpz937\Logutil\Log;
Log::info('This is an informational message');
Log::error('This is an error message');
?>
```
You can also set the log file path and the execution environment (CLI or not) using the setLogFile and setRunFromCli methods respectively.
### Running the tests
Explain how to run the automated tests for this system.
### Deployment
Add additional notes about how to deploy this on a live system.
### Built With
- adhocore/cli - Command line interface library for PHP
### License
This project is licensed under the MIT License - see the LICENSE file for details

20
composer.json Normal file
View File

@@ -0,0 +1,20 @@
{
"name": "hpz937/logutil",
"description": "logging utility for php",
"type": "library",
"license": "MIT",
"autoload": {
"psr-4": {
"Hpz937\\Logutil\\": "src/"
}
},
"authors": [
{
"name": "Brad Cimbura",
"email": "hpz937+code@gmail.com"
}
],
"require": {
"adhocore/cli": "^1.6"
}
}

89
composer.lock generated Normal file
View File

@@ -0,0 +1,89 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "c5bb6d2fc4a2ade259b305bb1dd9509c",
"packages": [
{
"name": "adhocore/cli",
"version": "v1.6.2",
"source": {
"type": "git",
"url": "https://github.com/adhocore/php-cli.git",
"reference": "34191315b0da20b9b4ecad783d91db992fa209a4"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/adhocore/php-cli/zipball/34191315b0da20b9b4ecad783d91db992fa209a4",
"reference": "34191315b0da20b9b4ecad783d91db992fa209a4",
"shasum": ""
},
"require": {
"php": ">=8.0"
},
"require-dev": {
"phpunit/phpunit": "^9.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Ahc\\Cli\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Jitendra Adhikari",
"email": "jiten.adhikary@gmail.com"
}
],
"description": "Command line interface library for PHP",
"keywords": [
"argument-parser",
"argv-parser",
"cli",
"cli-action",
"cli-app",
"cli-color",
"cli-option",
"cli-writer",
"command",
"console",
"console-app",
"php-cli",
"php8",
"stream-input",
"stream-output"
],
"support": {
"issues": "https://github.com/adhocore/php-cli/issues",
"source": "https://github.com/adhocore/php-cli/tree/v1.6.2"
},
"funding": [
{
"url": "https://paypal.me/ji10",
"type": "custom"
},
{
"url": "https://github.com/adhocore",
"type": "github"
}
],
"time": "2024-01-22T22:37:23+00:00"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": [],
"plugin-api-version": "2.6.0"
}

82
src/Log.php Normal file
View File

@@ -0,0 +1,82 @@
<?php
namespace Hpz937\Logutil;
use Ahc\Cli\Output\Color;
class Log {
private static $logFile = 'app.log';
/**
* Sets the file path for the log file.
*
* @param string $logFilePath Path to the log file.
*/
public static function setLogFile($logFilePath) {
self::$logFile = $logFilePath;
}
/**
* Logs an informational message.
*
* @param string $message The message to be logged.
*/
public static function info($message) {
self::writeLog("INFO", $message);
}
/**
* Logs an error message.
*
* @param string $message The error message to be logged.
*/
public static function error($message) {
self::writeLog("ERROR", $message);
}
/**
* Writes a log entry to either the console or a log file, depending on the execution environment.
*
* @param string $level The log level (e.g., 'INFO', 'ERROR').
* @param string $message The log message.
*/
private static function writeLog($level, $message) {
$formattedMessage = "[" . date('Y-m-d H:i:s') . "] $level: $message\n";
if (self::isCli()) {
self::outputToConsole($level, $formattedMessage);
} else {
file_put_contents(self::$logFile, $formattedMessage, FILE_APPEND);
}
}
/**
* Determines if the current execution environment is CLI (Command Line Interface).
*
* @return bool True if the script is running in a CLI environment, false otherwise.
*/
private static function isCli() {
return isset($_ENV['RUN_FROM_CLI']) && $_ENV['RUN_FROM_CLI'] === true;
}
/**
* Sets the value of the RUN_FROM_CLI environment variable.
*
* @param bool $value The value to set.
*/
public static function setRunFromCli($value) {
$_ENV['RUN_FROM_CLI'] = $value;
}
/**
* Outputs a formatted message to the console.
*
* @param string $level The log level (e.g., 'INFO', 'ERROR').
* @param string $message The message to be output.
*/
private static function outputToConsole($level, $message) {
$color = new Color();
$outputMessage = $level === 'ERROR' ? $color->red($message) : $color->green($message);
echo $outputMessage;
}
}