Development

/branches/1.0/lib/log/sfLogger/sfFileLogger.class.php

You must first sign up to be able to contribute.

root/branches/1.0/lib/log/sfLogger/sfFileLogger.class.php

Revision 10964, 1.8 kB (checked in by fabien, 6 years ago)

[1.0, 1.1, 1.2] fixed sfFileLogger file closing

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  *
13  * @package    symfony
14  * @subpackage log
15  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
16  * @version    SVN: $Id$
17  */
18 class sfFileLogger
19 {
20   protected
21     $fp = null;
22
23   /**
24    * Initializes the file logger.
25    *
26    * @param array Options for the logger
27    */
28   public function initialize($options = array())
29   {
30     if (!isset($options['file']))
31     {
32       throw new sfConfigurationException('File option is mandatory for a file logger');
33     }
34
35     $dir = dirname($options['file']);
36
37     if (!is_dir($dir))
38     {
39       mkdir($dir, 0777, 1);
40     }
41
42     $fileExists = file_exists($options['file']);
43     if (!is_writable($dir) || ($fileExists && !is_writable($options['file'])))
44     {
45       throw new sfFileException(sprintf('Unable to open the log file "%s" for writing', $options['file']));
46     }
47
48     $this->fp = fopen($options['file'], 'a');
49     if (!$fileExists)
50     {
51       chmod($options['file'], 0666);
52     }
53   }
54
55   /**
56    * Logs a message.
57    *
58    * @param string Message
59    * @param string Message priority
60    * @param string Message priority name
61    */
62   public function log($message, $priority, $priorityName)
63   {
64     $line = sprintf("%s %s [%s] %s%s", strftime('%b %d %H:%M:%S'), 'symfony', $priorityName, $message, DIRECTORY_SEPARATOR == '\\' ? "\r\n" : "\n");
65
66     flock($this->fp, LOCK_EX);
67     fwrite($this->fp, $line);
68     flock($this->fp, LOCK_UN);
69   }
70
71   /**
72    * Executes the shutdown method.
73    */
74   public function shutdown()
75   {
76     if (is_resource($this->fp))
77     {
78       fclose($this->fp);
79     }
80   }
81 }
82
Note: See TracBrowser for help on using the browser.