PageRenderTime 102ms CodeModel.GetById 60ms app.highlight 3ms RepoModel.GetById 38ms app.codeStats 0ms

/system/classes/Kohana/Log/File.php

https://bitbucket.org/chrispiechowicz/zepto
PHP | 94 lines | 38 code | 14 blank | 42 comment | 4 complexity | b2fd67d9452f1c4684b7d0e81f3bcc4f MD5 | raw file
 1<?php defined('SYSPATH') OR die('No direct script access.');
 2/**
 3 * File log writer. Writes out messages and stores them in a YYYY/MM directory.
 4 *
 5 * @package    Kohana
 6 * @category   Logging
 7 * @author     Kohana Team
 8 * @copyright  (c) 2008-2012 Kohana Team
 9 * @license    http://kohanaframework.org/license
10 */
11class Kohana_Log_File extends Log_Writer {
12
13	/**
14	 * @var  string  Directory to place log files in
15	 */
16	protected $_directory;
17
18	/**
19	 * Creates a new file logger. Checks that the directory exists and
20	 * is writable.
21	 *
22	 *     $writer = new Log_File($directory);
23	 *
24	 * @param   string  $directory  log directory
25	 * @return  void
26	 */
27	public function __construct($directory)
28	{
29		if ( ! is_dir($directory) OR ! is_writable($directory))
30		{
31			throw new Kohana_Exception('Directory :dir must be writable',
32				array(':dir' => Debug::path($directory)));
33		}
34
35		// Determine the directory path
36		$this->_directory = realpath($directory).DIRECTORY_SEPARATOR;
37	}
38
39	/**
40	 * Writes each of the messages into the log file. The log file will be
41	 * appended to the `YYYY/MM/DD.log.php` file, where YYYY is the current
42	 * year, MM is the current month, and DD is the current day.
43	 *
44	 *     $writer->write($messages);
45	 *
46	 * @param   array   $messages
47	 * @return  void
48	 */
49	public function write(array $messages)
50	{
51		// Set the yearly directory name
52		$directory = $this->_directory.date('Y');
53
54		if ( ! is_dir($directory))
55		{
56			// Create the yearly directory
57			mkdir($directory, 02777);
58
59			// Set permissions (must be manually set to fix umask issues)
60			chmod($directory, 02777);
61		}
62
63		// Add the month to the directory
64		$directory .= DIRECTORY_SEPARATOR.date('m');
65
66		if ( ! is_dir($directory))
67		{
68			// Create the monthly directory
69			mkdir($directory, 02777);
70
71			// Set permissions (must be manually set to fix umask issues)
72			chmod($directory, 02777);
73		}
74
75		// Set the name of the log file
76		$filename = $directory.DIRECTORY_SEPARATOR.date('d').EXT;
77
78		if ( ! file_exists($filename))
79		{
80			// Create the log file
81			file_put_contents($filename, Kohana::FILE_SECURITY.' ?>'.PHP_EOL);
82
83			// Allow anyone to write to log files
84			chmod($filename, 0666);
85		}
86
87		foreach ($messages as $message)
88		{
89			// Write each message into the log file
90			file_put_contents($filename, PHP_EOL.$this->format_message($message), FILE_APPEND);
91		}
92	}
93
94} // End Kohana_Log_File