Packages

  • dibi
    • drivers
    • nette
    • reflection
  • None
  • PHP

Classes

  • dibi
  • DibiConnection
  • DibiDataSource
  • DibiDateTime
  • DibiEvent
  • DibiFileLogger
  • DibiFirePhpLogger
  • DibiFluent
  • DibiObject
  • DibiResult
  • DibiResultIterator
  • DibiRow
  • DibiTranslator

Interfaces

  • IDataSource
  • IDibiDriver
  • IDibiReflector
  • IDibiResultDriver

Exceptions

  • DibiDriverException
  • DibiException
  • DibiNotImplementedException
  • DibiNotSupportedException
  • DibiPcreException
  • Overview
  • Package
  • Class
  • Tree
 1: <?php
 2: 
 3: /**
 4:  * This file is part of the "dibi" - smart database abstraction layer.
 5:  *
 6:  * Copyright (c) 2005 David Grudl (http://davidgrudl.com)
 7:  *
 8:  * For the full copyright and license information, please view
 9:  * the file license.txt that was distributed with this source code.
10:  */
11: 
12: 
13: 
14: /**
15:  * dibi file logger.
16:  *
17:  * @author     David Grudl
18:  * @package    dibi
19:  */
20: class DibiFileLogger extends DibiObject
21: {
22:     /** @var string  Name of the file where SQL errors should be logged */
23:     public $file;
24: 
25:     /** @var int */
26:     public $filter;
27: 
28: 
29: 
30:     public function __construct($file, $filter = NULL)
31:     {
32:         $this->file = $file;
33:         $this->filter = $filter ? (int) $filter : DibiEvent::QUERY;
34:     }
35: 
36: 
37: 
38:     /**
39:      * After event notification.
40:      * @return void
41:      */
42:     public function logEvent(DibiEvent $event)
43:     {
44:         if (($event->type & $this->filter) === 0) {
45:             return;
46:         }
47: 
48:         $handle = fopen($this->file, 'a');
49:         if (!$handle) return; // or throw exception?
50:         flock($handle, LOCK_EX);
51: 
52:         if ($event->result instanceof Exception) {
53:             $message = $event->result->getMessage();
54:             if ($code = $event->result->getCode()) {
55:                 $message = "[$code] $message";
56:             }
57:             fwrite($handle,
58:                 "ERROR: $message"
59:                 . "\n-- SQL: " . $event->sql
60:                 . "\n-- driver: " . $event->connection->getConfig('driver') . '/' . $event->connection->getConfig('name')
61:                 . ";\n-- " . date('Y-m-d H:i:s')
62:                 . "\n\n"
63:             );
64:         } else {
65:             fwrite($handle,
66:                 "OK: " . $event->sql
67:                 . ($event->count ? ";\n-- rows: " . $event->count : '')
68:                 . "\n-- takes: " . sprintf('%0.3f', $event->time * 1000) . ' ms'
69:                 . "\n-- source: " . implode(':', $event->source)
70:                 . "\n-- driver: " . $event->connection->getConfig('driver') . '/' . $event->connection->getConfig('name')
71:                 . "\n-- " . date('Y-m-d H:i:s')
72:                 . "\n\n"
73:             );
74:         }
75:         fclose($handle);
76:     }
77: 
78: }
79: 
dibi API documentation API documentation generated by ApiGen 2.3.0