Giter Club home page Giter Club logo

php-logger's Introduction

PHP Logger

Simple logger class to:

  • Keep track of log entries.
  • Keep track of timings with time() and timeEnd() methods, Javascript style.
  • Optionally write log entries in real-time to file or to screen.
  • Optionally dump the log to file in one go at any time.

Log entries can be added with any of the following methods:

  • info( $message, $title )
  • debug( $message, $title )
  • warning( $message, $title )
  • error( $message, $title )

For example, the following code

Logger::info( "an informational message intended for the user, ex: program started" );
Logger::debug( "a diagnostic message intended for the developer, ex: variable value = false" );
Logger::warning( "a warning that something might go wrong, ex: variable not set, something bad might happen" );
Logger::error( "explain why the program is going to crash, ex: file not found, exiting" );

will print to STDOUT the following lines:

$> 2018-04-09T16:21:34+02:00 [INFO] : an informational message intended for the user, ex: program started
$> 2018-04-09T16:21:34+02:00 [DEBUG] : a diagnostic message intended for the developer, ex: variable value = false
$> 2018-04-09T16:21:34+02:00 [WARNING] : a warning that something might go wrong, ex: variable not set, something bad might happen
$> 2018-04-09T16:21:34+02:00 [ERROR] : explain why the program is going to crash, ex: file not found, exiting

To write the same output to file, prepend the following line:

Logger::$write_log = true;

To customize the log file path:

Logger::$log_dir = 'logs';
Logger::$log_file_name = 'log';
Logger::$log_file_extension = 'log';

To overwrite the log file at every run of the script:

Logger::$log_file_append = false;

To prevent printing to screen:

Logger::$print_log = false;

Parallel code caveat

The class uses the static methods and internal flags (e.g. $logger_ready) to keep its state. We do this to make the class work straight away, without any previous configuration or the need to instantiate it. This however can create race conditions if you are executing parallel code. Please let us know if this is a problem for you, if we receive enough feedback, we will switch to a more class-like approach.

php-logger's People

Contributors

coccoinomane avatar ideariasrl avatar boghy933 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.