2 // vim: foldmethod=marker
6 * @author Masaki Fujimoto <fujimoto@php.net>
7 * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
12 // {{{ Ethna_Plugin_Logwriter_File
16 * @author Masaki Fujimoto <fujimoto@php.net>
20 class Ethna_Plugin_Logwriter_File extends Ethna_Plugin_Logwriter
26 /** @var int ログファイルハンドル */
29 /** @var int ログファイルパーミッション */
39 function Ethna_Plugin_Logwriter_File()
48 * @param int $option ログオプション(LOG_FILE,LOG_FUNCTION...)
50 function setOption($option)
52 parent::setOption($option);
54 if (isset($option['file'])) {
55 $this->file = $option['file'];
57 $this->file = $this->_getLogFile();
60 if (isset($option['mode'])) {
61 $this->mode = $option['mode'];
72 $this->fp = fopen($this->file, 'a');
73 $st = fstat($this->fp);
74 if (function_exists("posix_getuid") && posix_getuid() == $st[4]) {
75 chmod($this->file, intval($this->mode, 8));
83 * @param int $level ログレベル(LOG_DEBUG, LOG_NOTICE...)
84 * @param string $message ログメッセージ(+引数)
86 function log($level, $message)
88 if ($this->fp == null) {
92 $prefix = strftime('%Y/%m/%d %H:%M:%S ') . $this->ident;
93 if (array_key_exists("pid", $this->option)) {
94 $prefix .= sprintf('[%d]', getmypid());
96 $prefix .= sprintf('(%s): ', $this->_getLogLevelName($level));
97 if (array_key_exists("function", $this->option) ||
98 array_key_exists("pos", $this->option)) {
100 $bt = $this->_getBacktrace();
101 if ($bt && array_key_exists("function", $this->option) && $bt['function']) {
102 $tmp .= $bt['function'];
104 if ($bt && array_key_exists("pos", $this->option) && $bt['pos']) {
105 $tmp .= $tmp ? sprintf('(%s)', $bt['pos']) : $bt['pos'];
108 $prefix .= $tmp . ": ";
111 fwrite($this->fp, $prefix . $message . "\n");
113 return $prefix . $message;
130 * ログファイルの書き出し先を取得する(ログファシリティに
131 * LOG_FILEが指定されている場合のみ有効)
133 * ログファイルの書き出し先を変更したい場合はこのメソッドを
137 * @return string ログファイルの書き出し先
139 function _getLogFile()
141 $controller =& Ethna_Controller::getInstance();
143 if (array_key_exists("dir", $this->option)) {
144 $dir = $this->option['dir'];
146 $dir = $controller->getDirectory('log');
149 return sprintf('%s/%s.log',
151 strtolower($controller->getAppid())