Code Monkey home page Code Monkey logo

logger's Introduction

Logger

Logger is a PHP text file logging facility, implemented as a singleton. It's nothing special, but I'm breaking a larger code base down into independent components and I needed this to be in its own repository.

Usage

Implementing Logger in 4 easy steps:

  1. Either composer require parseword/logger or copy the Logger.php file into your project.

  2. Make sure Logger.php is discoverable by your autoloader, or require_once it manually.

  3. Throughout your application code, call Logger::debug(), Logger::info(), Logger::warning() and Logger::error() to send log messages of differing severity levels.

  4. In your application's config file or a common include file, you (or the user) call Logger::setFilename() to set the log file, and Logger::setSeverityFilter() to specify which log messages are written to disk.

Example

In the following example, a file named /tmp/my.log is created. The severity filter is set so that only messages with severity of WARNING or higher will be written. Messages with lower severity will be disregarded.

<?php
//Set up the logger in your config or global include file
use parseword\logger\Logger;
Logger::setFilename('/tmp/my.log');
Logger::setSeverityFilter(Logger::SEVERITY_WARNING);
Logger::setLabel('myCoolApp');

//Call the static Logger methods throughout your application code
Logger::info("Somebody set us up the bomb.");
Logger::debug("Main screen turn on.");
Logger::warning("All your base are belong to us.");
Logger::info("You have no chance to survive make your time.");
Logger::error("Unable to move 'ZIG', aborting");

The contents of /tmp/my.log will look like this:

[2019-01-25,14:40:13.797 CST]  WARN: myCoolApp: All your base are belong to us.
[2019-01-25,14:40:13.797 CST] ERROR: myCoolApp: Unable to move 'ZIG', aborting

Inspect or run the included test.php file for more examples.

Method overview

These methods are used to configure the Logger:

  • setCollapseEntries() - Whether or not to replace newlines in log entries with spaces, constraining each entry to a single line. Defaults to true.

  • setDateFormat() - Set the date format to use when stamping log messages.

  • setFilename() - Set the filesystem path and filename for the log file.

  • setLabel() - Set an optional text string to include in log messages. If each instance of your application has a unique ID, setting it here can help with tracing and troubleshooting.

  • setSeverityFilter() - Set which types of log messages are written to the file.

These methods control the logger's functionality:

  • debug() - Send a log message of severity DEBUG.

  • error() - Send a log message of severity ERROR.

  • warning() - Send a log message of severity WARNING.

  • info() - Send a log message of severity INFO.

  • truncate() - Truncate the log file, wiping out any previous entries.

Requirements

The Logger class requires PHP7 to support scalar type declarations.

Errata

None at this time.

logger's People

Contributors

parseword avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  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.