2017 © Pedro Peláez
 

framework logger

Alexya's Logger

image

alexya-framework/logger

Alexya's Logger

  • Tuesday, June 6, 2017
  • by manulaiko
  • Repository
  • 1 Watchers
  • 0 Stars
  • 36 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 0 % Grown

The README.md

Logger

Alexya's logger componets, (*1)

Contents

, (*2)

Abstract logger

\Alexya\Logger\AbstractLogger is the base class for all different loggers available. It is the class that checks which messages can be logged and formats them., (*3)

Its constructor accepts the following parameters:, (*4)

  • A string being the format that each log message will have.
  • An array with the log levels that the logger can log.

The string parameter can contain placeholders for formatting the message, this are the available placeholders:, (*5)

  • {YEAR}, current year.
  • {MONTH}, current month.
  • {DAY}, current day.
  • {HOUR}, current hour.
  • {MINUTE}, current minute.
  • {SECOND}, current second.
  • {SERVER_NAME}, server's name (localhost, test.com...).
  • {CALLING_FUNCTION}, the function that called the logger.
  • {CALLING_FILE}, the file that called the logger.
  • {CALLING_LINE}, the line that called the logger.
  • {CALLING_CLASS}, the class that called the logger.
  • {CALLING_TYPE}, -> if the logger was called by an object, :: if it was called statically.
  • {LEVEL}, the level on which the log has been called.
  • {LOG}, the string to log.

If the string is empty the logger will use [{HOUR}:{MINUTE}] ({LEVEL}) {LOG} as format., (*6)

The array parameter contains the levels that the logger can log, if it's empty it will log all levels. This are supported levels:, (*7)

  • emergency
  • alert
  • critical
  • error
  • warning
  • notice
  • info
  • debug

The class \Psr\Log\LogLevel contains the constants definitions for all levels., (*8)

The log method will first see if the log message can be logged and then format it (if it can be logged), it accepts three parameters:, (*9)

  • A string being the level on which the message should be logged.
  • A string being the message to log (can contain placeholders).
  • An array containing custom placeholders.

If the level isn't any of the costants defined in \Psr\Log\LogLevel it will throw an exception of type \Psr\Log\InvalidArgumentException., (*10)

All classes that extends this class must implement the method _write which will write the log message to wherever the child class wants. It accepts as parameter the formatted message and an the placeholders array:, (*11)

<?php
namespace Test;

use \Alexya\Logger\AbstractLogger;

/**
 * Simple logger that echoes each log entry
 */
class Logger extends AbstractLogger
{
    protected function _write($message, $placeholders)
    {
        echo $message;
    }
}

, (*12)

File logger

The class \Alexya\Logger\File outputs each log message to a file., (*13)

The constructor accepts four parameters:, (*14)

  • The \Alexya\FileSystem\File object that will represent the path where the log files will be saved.
  • A string being the format of each log file name, you can add the followin placeholders:
  • A string being the format that each log entry will have.
  • An array containing the elements that will be logged, you can get a full list of available values in the class \Psr\Log\LogLevel.

The file name format can have the followin placeholders:, (*15)

  • {YEAR}, current year.
  • {MONTH}, current month.
  • {DAY}, current day.
  • {HOUR}, current hour.
  • {MINUTE}, current minute.
  • {SECOND}, current second.
  • {SERVER_NAME}, server's name (localhost, test.com...).

If the file doesn't exist in the moment the message is going to be logged it will be created., (*16)

<?php
$Logger = new \Alexya\Logger\File(
    new \Alexya\FileSystem\Direcctory("/tmp/log/Alexya"),
    "{YEAR}-{MONTH}-{DAY}.log",
    "[{HOUR}:{MINUTE}] ({LEVEL}) {LOG}",
    [
        \Psr\Log\LogLevel::EMERGENCY,
        \Psr\Log\LogLevel::ALERT,
        \Psr\Log\LogLevel::CRITICAL,
        \Psr\Log\LogLevel::ERROR,
        \Psr\Log\LogLevel::WARNING,
        \Psr\Log\LogLevel::NOTICE,
        \Psr\Log\LogLevel::INFO,
        \Psr\Log\LogLevel::DEBUG
    ]
);

$Logger->debug("test"); // [00:00] (debug) test
$Logger->info("[{HOUR}:{MINUTE}] ({LEVEL}) {CUSTOM_PLACEHOLDER}", [
    "CUSTOM_PLACEHOLDER" => "test"
]); // [00:00] (debug) test

, (*17)

Database logger

The class \Alexya\Logger\Database stores each log message in a database table., (*18)

The constructor acccepts five parameters:, (*19)

  • The \Alexya\Database\Connection object that will be used for interacting with the database.
  • A string being the table name.
  • An associative array containing the columns and the values to insert.
  • A string being the format that each log entry will have.
  • An array containing the elements that will be logged, you can get a full list of available values in the class \Psr\Log\LogLevel.

Both, the columns array and the format string can accept the followin placeholders:, (*20)

  • {YEAR}, current year.
  • {MONTH}, current month.
  • {DAY}, current day.
  • {HOUR}, current hour.
  • {MINUTE}, current minute.
  • {SECOND}, current second.
  • {SERVER_NAME}, server's name (localhost, test.com...).
  • {CALLING_FUNCTION}, the function that called the logger.
  • {CALLING_FILE}, the file that called the logger.
  • {CALLING_LINE}, the line that called the logger.
  • {CALLING_CLASS}, the class that called the logger.
  • {CALLING_TYPE}, -> if the logger was called by an object, :: if it was called statically.
  • {LEVEL}, the level on which the log has been called.
  • {LOG}, the string to log.
<?php
$Logger = new \Alexya\Logger\Database(
    $Database,
    "logs",
    [
        "date"    => "{YEAR}-{MONTH}-{DAY} {HOUR}:{MINUTE}:{SECOND}",
        "caller"  => "{CALLER_CLASS}{CALLER_TYPE}{CALLER_FUNCTION} ({CALLER_FILE}:{CALLER_LINE})",
        "level"   => "{LEVEL}",
        "message" => "{LOG}"
    ],
    [
        \Psr\Log\LogLevel::EMERGENCY,
        \Psr\Log\LogLevel::ALERT,
        \Psr\Log\LogLevel::CRITICAL,
        \Psr\Log\LogLevel::ERROR,
        \Psr\Log\LogLevel::WARNING,
        \Psr\Log\LogLevel::NOTICE,
        \Psr\Log\LogLevel::INFO,
        \Psr\Log\LogLevel::DEBUG
    ]
);

$Logger->debug("test"); // INSERT INTO `logs` (`date`, `caller`, `level`, `message`) VALUES ('0000-00-00 00:00:00', '', 'debug', 'test');
$Logger->info("test", [
    "date"    => "{HOUR}:{MINUTE}:{SECOND}",
    "caller"  => "{CALLER_CLASS}{CALLER_TYPE}{CALLER_FUNCTION} ({CALLER_FILE}:{CALLER_LINE})",
    "level"   => "{LEVEL}",
    "message" => "{LOG}"
]); // INSERT INTO `logs` (`date`, `caller`, `level`, `message`) VALUES ('00:00:00', '', 'debug', 'test');

The Versions

06/06 2017

dev-develop

dev-develop

Alexya's Logger

  Sources   Download

GNU

The Requires

 

by Avatar manulaiko

logger framework php php7 alexya

05/06 2017

dev-master

9999999-dev

Alexya's Logger

  Sources   Download

GNU

The Requires

 

by Avatar manulaiko

logger framework php php7 alexya

05/06 2017

3.0.5

3.0.5.0

Alexya's Logger

  Sources   Download

GNU

The Requires

 

by Avatar manulaiko

logger framework php php7 alexya

25/08 2016

3.0.4

3.0.4.0

Alexya's Logger

  Sources   Download

GNU

The Requires

 

by Avatar manulaiko

logger framework php php7 alexya

25/08 2016

3.0.3

3.0.3.0

Alexya's Logger

  Sources   Download

GNU

The Requires

 

by Avatar manulaiko

logger framework php php7 alexya

25/08 2016

3.0.2

3.0.2.0

Alexya's Logger

  Sources   Download

GNU

The Requires

 

by Avatar manulaiko

logger framework php php7 alexya

16/08 2016

3.0.1

3.0.1.0

Alexya's Logger

  Sources   Download

GNU

The Requires

 

by Avatar manulaiko

logger framework php php7 alexya

12/08 2016

3.0.0

3.0.0.0

Alexya's Logger

  Sources   Download

GNU

The Requires

 

by Avatar manulaiko

logger framework php php7 alexya