Skip to content

Commit

Permalink
Merge pull request #5 from nerds-and-company/feature/better_log_proce…
Browse files Browse the repository at this point in the history
…ssing

Better log processing
  • Loading branch information
Bob Olde Hampsink authored Feb 23, 2017
2 parents e7b304d + 6c2e255 commit 2c5050f
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 6 deletions.
2 changes: 2 additions & 0 deletions LogHelperPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@ public function init()

// Use STDERR logging?
if (craft()->config->get('useStdErrLog', 'logHelper')) {
require_once __DIR__.'/logging/LogHelper_BaseLogRoute.php';
require_once __DIR__.'/logging/LogHelper_StdErrLogRoute.php';
craft()->log->addRoute('Craft\LogHelper_StdErrLogRoute');
}

// Use SysLog logging?
if (craft()->config->get('useSysLog', 'logHelper')) {
require_once __DIR__.'/logging/LogHelper_BaseLogRoute.php';
require_once __DIR__.'/logging/LogHelper_SysLogRoute.php';
craft()->log->addRoute('Craft\LogHelper_SysLogRoute');
}
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ This plugin has been licensed under the MIT License (MIT). Please see [License F

## Changelog

### 2.0.3

- Better log processing

### 2.0.2

- Fixed bug with reading default config values
Expand Down
76 changes: 76 additions & 0 deletions logging/LogHelper_BaseLogRoute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Craft;

/**
* Log Helper Plugin.
*
* Advanced logging options
*
* @author Nerds & Company
* @copyright Copyright (c) 2016, Nerds & Company
*
* @link https://www.nerds.company
*/
abstract class LogHelper_BaseLogRoute extends \CLogRoute
{
// Public Methods
// =========================================================================

/**
* Initializes the log route. This method is invoked after the log route is created by the route manager.
*
* @return null
*/
public function init()
{
$this->levels = craft()->config->get('devMode') ? '' : 'error,warning';
$this->filter = craft()->config->get('devMode') ? 'Craft\\LogFilter' : null;

parent::init();
}

// Protected Methods
// =========================================================================

/**
* Saves log messages in files.
*
* @param array $logs The list of log messages
*
* @return null
*/
protected function processLogs($logs)
{
$processed = array();

foreach ($logs as $log) {
$message = LoggingHelper::redact($log[0]);
$level = $log[1];
$category = $log[2];
$time = $log[3];
$force = (isset($log[4]) && $log[4] == true) ? true : false;
$plugin = isset($log[5]) ? StringHelper::toLowerCase($log[5]) : 'craft';

$processed[] = $this->formatLogMessageWithForce($message, $level, $category, $time, $force, $plugin);
}

return $processed;
}

/**
* Formats a log message given different fields.
*
* @param string $message The message content.
* @param int $level The message level.
* @param string $category The message category.
* @param int $time The message timestamp.
* @param bool $force Whether the message was forced or not.
*
* @return string The formatted message.
*/
protected function formatLogMessageWithForce($message, $level, $category, $time, $force)
{
return "[$level] [$category]".($force ? " [Forced]" : "")." $message";
}
}
8 changes: 5 additions & 3 deletions logging/LogHelper_StdErrLogRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@
*
* @link https://www.nerds.company
*/
class LogHelper_StdErrLogRoute extends \CLogRoute
class LogHelper_StdErrLogRoute extends LogHelper_BaseLogRoute
{
/**
* Process logs.
*
* @param array $logs
*/
public function processLogs($logs)
protected function processLogs($logs)
{
$logs = parent::processLogs($logs);

$strErr = fopen('php://stderr', 'w');

foreach ($logs as $log) {
fwrite($strErr, $log[0]."\n");
fwrite($strErr, $log."\n");
}

fclose($strErr);
Expand Down
8 changes: 5 additions & 3 deletions logging/LogHelper_SysLogRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@
*
* @link https://www.nerds.company
*/
class LogHelper_SysLogRoute extends \CLogRoute
class LogHelper_SysLogRoute extends LogHelper_BaseLogRoute
{
/**
* Process logs.
*
* @param array $logs
*/
public function processLogs($logs)
protected function processLogs($logs)
{
$logs = parent::processLogs($logs);

foreach ($logs as $log) {
syslog(LOG_DEBUG, $log[0]."\n");
syslog(LOG_DEBUG, $log."\n");
}
}
}

0 comments on commit 2c5050f

Please sign in to comment.