Skip to content

Commit

Permalink
new base class Log/Engine/BaseLog
Browse files Browse the repository at this point in the history
  • Loading branch information
rchavik committed May 11, 2012
1 parent aab1eb6 commit 49bc818
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 10 deletions.
61 changes: 61 additions & 0 deletions lib/Cake/Log/Engine/BaseLog.php
@@ -0,0 +1,61 @@
<?php
/**
* Base Log Engine class
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
* @package Cake.Log.Engine
* @since CakePHP(tm) v 2.2
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

App::uses('CakeLogInterface', 'Log');

/**
* Base log engine class.
*
* @package Cake.Log.Engine
*/
abstract class BaseLog implements CakeLogInterface {

/**
* Engine config
*
* @var string
*/
protected $_config = array();

/**
* __construct method
*
* @return void
*/
public function __construct($config = array()) {
$this->config($config);
}

/**
* Sets instance config. When $config is null, returns config array
*
* @param array $config engine configuration
* @return array
*/
public function config($config = array()) {
if (!empty($config)) {
if (isset($config['types']) && is_string($config['types'])) {
$config['types'] = array($config['types']);
}
$this->_config = $config;
}
return $this->_config;
}

}
26 changes: 19 additions & 7 deletions lib/Cake/Log/Engine/FileLog.php
Expand Up @@ -17,15 +17,15 @@
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

App::uses('CakeLogInterface', 'Log');
App::uses('BaseLog', 'Log/Engine');

/**
* File Storage stream for Logging. Writes logs to different files
* based on the type of log it is.
*
* @package Cake.Log.Engine
*/
class FileLog implements CakeLogInterface {
class FileLog extends BaseLog {

/**
* Path to save log files on.
Expand All @@ -37,15 +37,25 @@ class FileLog implements CakeLogInterface {
/**
* Constructs a new File Logger.
*
* Options
* Config
*
* - `path` the path to save logs on.
*
* @param array $options Options for the FileLog, see above.
*/
public function __construct($options = array()) {
$options += array('path' => LOGS);
$this->_path = $options['path'];
public function __construct($config = array()) {
parent::__construct($config);
$config = Set::merge(array(
'path' => LOGS,
'file' => null,
'types' => null,
), $this->_config);
$config = $this->config($config);
$this->_path = $config['path'];
$this->_file = $config['file'];
if (!empty($this->_file) && !preg_match('/\.log$/', $this->_file)) {
$this->_file .= '.log';
}
}

/**
Expand All @@ -58,7 +68,9 @@ public function __construct($options = array()) {
public function write($type, $message) {
$debugTypes = array('notice', 'info', 'debug');

if ($type == 'error' || $type == 'warning') {
if (!empty($this->_file)) {
$filename = $this->_path . $this->_file;
} elseif ($type == 'error' || $type == 'warning') {
$filename = $this->_path . 'error.log';
} elseif (in_array($type, $debugTypes)) {
$filename = $this->_path . 'debug.log';
Expand Down
5 changes: 3 additions & 2 deletions lib/Cake/Test/test_app/Lib/Log/Engine/TestAppLog.php
Expand Up @@ -17,10 +17,11 @@
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

App::uses('CakeLogInterface', 'Log');
App::uses('BaseLog', 'Log/Engine');

class TestAppLog implements CakeLogInterface {
class TestAppLog extends BaseLog {

public function write($type, $message) {
}

}
Expand Up @@ -16,8 +16,12 @@
* @since CakePHP(tm) v 1.3
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class TestPluginLog implements CakeLogInterface {

App::uses('BaseLog', 'Log/Engine');

class TestPluginLog extends BaseLog {

public function write($type, $message) {
}

}

0 comments on commit 49bc818

Please sign in to comment.