PHPackages                             swf/slf4php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Logging &amp; Monitoring](/categories/logging)
4. /
5. swf/slf4php

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

swf/slf4php
===========

SWF logging facade for php - inspired by slf4j / logback / log4j java projects

1.0.0(10y ago)1141Apache-2.0PHPPHP &gt;= 5.3.0

Since Feb 23Pushed 8y ago1 watchersCompare

[ Source](https://github.com/ironhawk/swf-slf4php)[ Packagist](https://packagist.org/packages/swf/slf4php)[ Docs](https://github.com/ironhawk/swf-slf4php)[ RSS](/packages/swf-slf4php/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependencies (4)Versions (3)Used By (0)

CI master: [ ![Codeship Status for ironhawk/swf-slf4php](https://camo.githubusercontent.com/16253757606c75b79b20d7da3e079c008492e9965e74a9e60391ca2f29f84d66/68747470733a2f2f636f6465736869702e636f6d2f70726f6a656374732f65346263383734302d626332382d303133332d313663302d3732643839353861646261362f7374617475733f6272616e63683d6d6173746572)](https://codeship.com/projects/136107)

What is this project?
=====================

[](#what-is-this-project)

**slf4php** is **similar** to the [slf4j](http://www.slf4j.org/) (The Simple Logging Facade for Java) project.

**slf4php** is not a logging implementation! **This is** more like **an abstraction layer** on the top of existing logging frameworks like [Monolog](https://github.com/Seldaek/monolog) - just like [slf4j](http://www.slf4j.org/)

If you are not adding (configuring) any logging implementation no problem! **slf4php** will default itself to the "no operation" mechanism. In terms of using the facade in your code nothing will change. And you can configure the logging any time later!

Supported logging implementations
=================================

[](#supported-logging-implementations)

This first 1.0 version supports [Monolog](https://github.com/Seldaek/monolog) logging implementation out of the box by providing `MonologProxyAppender` class.

Probably there will be more in upcoming versions.

The concept - in a nut shell
============================

[](#the-concept---in-a-nut-shell)

We have the following key entities:

- **Appenders**
    You might consider an `Appender` as an output channel. Writes something out to somewhere somehow... (file, database, e-mail, etc.)
    This is the point where an existing logging framework comes to the picture! This project doesn't have own `Appender` implementations. Rather doing that it just gives you "connectors" (typically following the proxy / adapter design patterns) to let you use your favorite logging framework(s).
- **Loggers**
    In your code you need a `Logger` instance to log messages. You can invoke the appropriate log method (info(), debug(), warning(), etc.) on it - as defined by the `LoggerInterface` in [PSR/log](https://github.com/php-fig/log)
    Loggers have **log level**. AND... They **have one or more Appenders** behind them! When you log something with your Logger instance this is routed to all the Appenders behind the Logger. So as a result your log message is written to a file, database or sent via e-mail. OR all together!
- **LoggerFactory**
    We have a `LoggerFactory`. You ask for the `Logger` instance from the factory - by providing the fully qualified class name of your class you want to do logging from. You get back a `Logger` instance - matching for the logging configuration you have provided to the `LoggerFactory` beforehand.

And: the configuration
======================

[](#and-the-configuration)

We want to be able to easily configure all the above! To achieve this the best way is having a simple (as possible) **configuration file**.

Take a quick look on the following JSON config and you will immediatelly understand the concept:

```
{
	"appenders" : [
		{
			"name" : "logFile",
			"builderClass" : "swf\\slf4php\\config\\builder\\monolog\\MonologProxyAppenderBuilder",
			"handlers" : [
				{
					"builderClass" : "swf\\slf4php\\config\\builder\\monolog\\MonologStreamHandlerBuilder",
					"stream" : "${LOG_DIR}/application.log",
					"formatter": {
						"builderClass" : "swf\\slf4php\\config\\builder\\monolog\\MonologLineFormatterBuilder",
						"format" : "[%datetime%] %extra.loggerName%.%level_name%: %message% %context% %extra%\n\n"
					}
				}
			]
		},
		{
			"name" : "console",
			"builderClass" : "swf\\slf4php\\config\\builder\\monolog\\MonologProxyAppenderBuilder",
			"handlers" : [
				{
					"builderClass" : "swf\\slf4php\\config\\builder\\monolog\\MonologStreamHandlerBuilder",
					"stream" : "php://stdout",
					"formatter": {
						"builderClass" : "swf\\slf4php\\config\\builder\\monolog\\MonologLineFormatterBuilder",
						"format" : "[%datetime%] %extra.loggerName%.%level_name%: %message% %context% %extra%\n\n"
					}
				}
			]
		}
	],

	"loggers" : [
		{
			"name" : "your.namespace.A",
			"level" : "DEBUG",
			"appenders" : ["logFile", "console"]
		},
		{
			"name" : "your.namespace.B",
			"level" : "INFO",
			"appenders" : "logFile"
		},
		{
			"level" : "WARNING",
			"appenders" : "logFile"
		}
	]
}
```

With the above simple config we have:

- Created two Appenders: a log file and a console. Using [Monolog](https://github.com/Seldaek/monolog) `StreamHandler`
- Created a DEBUG level Logger for "your.namespace.A" which is using both the log file Appender and the console Appender
- Created an INFO level Logger for "your.namespace.B" which is using the log file Appender only
- And created a WARNING level Logger using the log file Appender too. AND... You can notice that this one has no namespace definition... Which means that this is the *default* logger setup

> **VISIT OUR WIKI!** For more info about configuring the logging please visit the [Wiki page](https://github.com/ironhawk/swf-slf4php/wiki)!

Basic usage
===========

[](#basic-usage)

Assuming you have the above config saved in file named **log.config.json** you can configure the `LoggerFactory` like this:

```
