GithubHelp home page GithubHelp logo

aldairnatan / analog Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jbroadway/analog

0.0 2.0 0.0 408 KB

PHP 5.3+ micro logging package that is highly extendable via closures.

License: MIT License

PHP 100.00%

analog's Introduction

Analog - PHP 5.3+ micro logging package Build Status

Click here to lend your support to: Analog and make a donation at www.pledgie.com !

A MicroPHP logging package based on the idea of using closures for configurability and extensibility. It functions as a static class, but you can completely control the writing of log messages through a closure function (aka anonymous functions), or use the Analog\Logger wrapper that implements the PSR-3 specification.

By default, this class will write to a file named sys_get_temp_dir() . '/analog.txt' using the format "machine - date - level - message\n", making it usable with no customization necessary.

Analog also comes with over a dozen pre-written handlers in the Analog/Handlers folder, with examples for each in the examples folder. These include:

  • Amon - Send logs to the Amon server monitoring tool
  • Buffer - Buffer messages to send all at once (works with File, Mail, Stderr, and Variable handlers)
  • ChromeLogger - Sends messages to Chrome Logger browser plugin
  • File - Append messages to a file
  • FirePHP - Send messages to FirePHP browser plugin
  • GELF - Send message to the Graylog2 log management server
  • Ignore - Do nothing
  • LevelBuffer - Buffer messages and send only if sufficient error level reached
  • Mail - Send email notices
  • Mongo - Save to MongoDB collection
  • Multi - Send different log levels to different handlers
  • Post - Send messages over HTTP POST to another machine
  • Stderr - Send messages to STDERR
  • Syslog - Send messages to syslog
  • Threshold - Only writes log messages above a certain threshold
  • Variable - Buffer messages to a variable reference.

So while it's a micro class, it's highly extensible and very capable out of the box too.

Rationale

I wrote this because I wanted something very small and simple like KLogger, and preferably not torn out of a wider framework if possible. After searching, I wasn't happy with the single-purpose libraries I found. With KLogger for example, I didn't want an object instance but rather a static class, and I wanted more flexibility in the back-end.

I also found some that had the flexibility also had more complexity, for example Monolog is 25 source files (not incl. tests). With closures, this seemed to be a good balance of small without sacrificing flexibility.

What about Analog, the logfile analyzer? Well, since it hasn't been updated since 2004, I think it's safe to call a single-file PHP logging class the same thing without it being considered stepping on toes :)

Usage

Basic usage, with a custom handler function:

<?php

require_once 'Analog.php';

// Default logging to /tmp/analog.txt
Analog::log ('Log this error');

// Log to a MongoDB log collection
Analog::handler (function ($info) {
	static $conn = null;
	if (! $conn) {
		$conn = new Mongo ('localhost:27017');
	}
	$conn->mydb->log->insert ($info);
});

// Log an alert
Analog::log ('The sky is falling!', Analog::ALERT);

// Log some debug info
Analog::log ('Debugging info', Analog::DEBUG);

?>

Usage with PSR-0, Composer, and the FirePHP handler:

1. Create a composer.json file in the root of your project with the following contents.

{
	"require": {
		"analog/analog": "dev-master"
	}
}

2. Run php composer.phar install from the terminal in the root of your project.

3. Include Composer's autoloader and use the Analog\Analog class.

<?php

require_once 'vendor/autoload.php';

use Analog\Analog;

Analog::log ('Log this error');

Analog::handler (\Analog\Handler\FirePHP::init ());

Analog::log ('Take me to your browser');

?>

Usage with PSR-3, Composer, and the Variable handler:

1. Create a composer.json file in the root of your project with the following contents.

{
	"require": {
		"analog/analog": "dev-master"
	}
}

2. Run php composer.phar install from the terminal in the root of your project.

3. Include Composer's autoloader and use the Analog\Logger class.

<?php

require_once 'vendor/autoload.php';

$logger = new Analog\Logger;

$log = '';

$logger->handler (Analog\Handler\Variable::init ($log));

$logger->alert ('Things are really happening right now!');

var_dump ($log);

?>

For more examples, see the examples folder.

analog's People

Contributors

jbroadway avatar lord2800 avatar mzaweb avatar polyfractal avatar sponno avatar spronkey avatar tessus avatar

Watchers

 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.