0x20h/monoconf

此包已被弃用且不再维护。没有推荐替代包。

为 monolog 框架提供的类似 log4j 的日志配置

0.1.1 2013-06-06 20:25 UTC

This package is not auto-updated.

Last update: 2024-07-02 06:35:37 UTC


README

Monoconf

monolog 日志框架提供的类似 log4j 的配置。

使用方法

  1. 创建配置 JSON 文件
{
	"rules": {
        "*": {
            "error": {
                "handler": ["error-handler"]
            }
        },
		"MyApp\\Controller*": {
			"debug": {
                "handler": ["debug-handler"],
                "processor": ["pid"]
            },
            "error": {
                "handler": ["error-handler"]
            }
		}
	},
	"handler": {
		"error-handler": {
			"type": "Monolog\\Handler\\StreamHandler",
			"args": [
				"/my/app/error.log"
			],
			"formatter": "line"
		},
		"debug-handler": {
			"type": "Monolog\\Handler\\StreamHandler",
			"args": [
				"/my/app/application.log"
			],
			"formatter": "line"
		}
	},
    "formatter": {
        "line": {
			"type": "Monolog\\Formatter\\LineFormatter",
			"args": [
				"%datetime% %pid% %channel%@%level_name% %message% %context%\n"
			]
        }
	},
    "processor": {
        "pid": {
            "type": "Monolog\\Processor\\ProcessIdProcessor",
            "args": [
            ]
        }
    }
}
  1. 在您的应用程序中
require 'vendor/autoload.php';

use Monoconf\Monoconf;

// initialize monoconf
Monoconf::config(json_decode(file_get_contents('monoconf.json'), true));
namespace MyApp\Controller;

class SomeController {

    protected $Log;

    public function __construct() {
        self::$Log = \Monoconf\Monoconf::getLogger(__CLASS__);
    }

    public function someAction()
    {
        self::$Log->debug(__METHOD__.' called');
    }
}

使用此设置,MyApp\Controller 命名空间中的每个类都将获得一个记录器,它将记录到 /my/app/error.logdebug 级别的消息,而其他每个类将获得一个记录器,它将记录到 /my/app/error.logerror 级别的消息。

测试

phpunit --bootstrap tests/bootstrap.php tests/