Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* Howdenio Monolog extension
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @category Howdenio
* @package Howdenio_Monolog
* @copyright Copyright (c) 2015 Howden.io
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\ChromePHPHandler;
/**
* The purpose of this class is to interface between the Monolog Library and Magento's normal logging process.
*
* Class Howdenio_Log_Writer_Monolog
*/
class Howdenio_Log_Writer_Monolog extends Zend_Log_Writer_Abstract
{
const ERR_ZEND_LOG_MISSING = 'Zend log level %s does not exist';
const MAGENTO_CHANNEL = 'MAGENTO';
const VENDOR_FOLDER = 'vendor';
const VENDOR_AUTOLOAD = 'autoload.php';
// The file being logged to.
protected $_file = null;
/** @var Logger */
protected $_logger = null;
// See: http://tools.ietf.org/html/rfc5424 for a description of the different log levels
protected $_logLevelMap = array(
Zend_Log::DEBUG => Logger::DEBUG,
Zend_Log::INFO => Logger::INFO,
Zend_Log::NOTICE => Logger::NOTICE,
Zend_Log::WARN => Logger::WARNING,
Zend_Log::ERR => Logger::ERROR,
Zend_Log::CRIT => Logger::CRITICAL,
Zend_Log::ALERT => Logger::ALERT,
Zend_Log::EMERG => Logger::EMERGENCY
);
public function __construct($file)
{
$this->setFile($file);
}
/**
* Fetches the file to log against
*
* @return null
*/
protected function getFile()
{
return $this->_file;
}
/**
* Gets the appropriate Logger level depending on the Zend Level
*
* @param int $zendLogLevel
* @throws Exception if the $ZendLogLevel doesn't exist in the map
*/
protected function getLevel($zendLogLevel)
{
if (isset($this->_logLevelMap[$zendLogLevel])) {
return $this->_logLevelMap[$zendLogLevel];
} else {
throw new Exception(sprintf(self::ERR_ZEND_LOG_MISSING, $zendLogLevel));
}
}
protected function getLogger()
{
if (!$this->_logger instanceof Logger) {
$this->_logger = $this->constructLogger();
}
return $this->_logger;
}
/**
* Builds the instance of Logger with the appropriate handles
*
* @todo: Make the pushHandlers configurable in the admin panel.
* @return Logger
*/
protected function constructLogger()
{
$logger = new Logger(self::MAGENTO_CHANNEL);
$logger->pushHandler(new StreamHandler($this->getFile(), Logger::DEBUG));
$logger->pushHandler(new ChromePHPHandler());
return $logger;
}
/**
* Set the file used by the logger.
*
* @param string $file
* @return $this
*/
protected function setFile($file = 'system.log')
{
$this->_file = $file;
return $this;
}
/**
* Interface that Magento uses to output logs.
*
* @param array $event
*/
protected function _write($event)
{
$log = $this->getLogger();
$log->addRecord(
$this->getLevel($event['priority']),
$event['message']
);
}
/**
* @Todo: This doesn't work.
*
* @param array|Zend_Config $file
* @return Howdenio_Log_Writer_Monolog
*/
public static function factory($file)
{
return new self($file[0]);
}
}
<?xml version="1.0"?>
<!--
/**
* Howdenio Monolog extension
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @category Howdenio
* @package Howdenio_Monolog
* @copyright Copyright (c) 2015 Howden.io
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
-->
<config>
<modules>
<Howdenio_Monolog>
<active>true</active>
<codePool>local</codePool>
</Howdenio_Monolog>
</modules>
</config>
<?xml version="1.0"?>
<!--
/**
* Howdenio Monolog extension
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @category Howdenio
* @package Howdenio_Monolog
* @copyright Copyright (c) 2015 Howden.io
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
-->
<config>
<modules>
<Howdenio_Monolog>
<version>0.0.1</version>
</Howdenio_Monolog>
</modules>
<global>
<log>
<core>
<writer_model>Howdenio_Log_Writer_Monolog</writer_model>
</core>
</log>
</global>
<frontend>
<routers>
<howdenio>
<use>standard</use>
<args>
<module>Howdenio_Monolog</module>
<frontName>monolog</frontName>
</args>
</howdenio>
</routers>
</frontend>
</config>
@andrewhowdencom

This comment has been minimized.

Copy link
Owner Author

commented Mar 25, 2015

This is a mess; It's only meant for personal notes. Well done if you find it!

@andrewhowdencom

This comment has been minimized.

Copy link
Owner Author

commented Mar 25, 2015

Use the file name as the log tag (channel?)

@andrewhowdencom

This comment has been minimized.

Copy link
Owner Author

commented Mar 25, 2015

Develop intent document

@andrewhowdencom

This comment has been minimized.

Copy link
Owner Author

commented Mar 25, 2015

See if its possible not to autoload from lib

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.