Class LoggerAppenderPDO

Description

Appends log events to a db table using PDO.

Configurable parameters of this appender are:

  • user - Sets the user of this database connection
  • password - Sets the password of this database connection
  • createTable - true, if the table should be created if necessary. false otherwise
  • table - Sets the table name (default: log4php_log)
  • sql - Sets the insert statement for a logging event. Defaults to the correct one - change only if you are sure what you are doing.
  • dsn - Sets the DSN string for this connection
If $sql is set then $table and $sql are used, else $table, $insertSql and $insertPattern.

An example:

  1. require_once dirname(__FILE__).'/../../main/php/Logger.php';
  2.  
  3. Logger::configure(dirname(__FILE__).'/../resources/appender_pdo.properties');
  4. $logger Logger::getRootLogger();
  5. $logger->fatal("Hello World!");

  1. log4php.rootLogger = DEBUG, a1, a2, a3
  2.  
  3. ; The table is created if necessary and filled using prepared statements.  
  4. log4php.appender.a1 = LoggerAppenderPDO
  5. log4php.appender.a1.dsn = "sqlite:target/appender_pdo.sqlite"
  6.  
  7. ; The following shows an appender with customized INSERT statment and table name. 
  8. log4php.appender.a2 = LoggerAppenderPDO
  9. log4php.appender.a2.user = root
  10. log4php.appender.a2.password = secret
  11. log4php.appender.a2.dsn = "mysql:host=localhost;dbname=test"
  12. log4php.appender.a2.table = log2
  13. log4php.appender.a2.insertSql = "INSERT INTO log2 (timestamp, logger, level, message, thread, file, line) VALUES (?,?,?,?,?,?,?)"
  14. log4php.appender.a2.insertPattern = "%d,%c,%p,%m, %t,%F,%L"
  15.  
  16. ; DEPRECATED: Using old style LoggerPatternLayout is considered unsafe as %m can contain quotes that mess up the SQL! 
  17. log4php.appender.a3 = LoggerAppenderPDO
  18. log4php.appender.a3.dsn = "sqlite:target/appender_pdo.sqlite"
  19. log4php.appender.a3.table = log3
  20. log4php.appender.a3.sql = "INSERT INTO log3 (timestamp, level, message) VALUES ('%t', '%p', '%m')"

  • version: $Revision: 806678 $
  • since: 2.0

Located in /appenders/LoggerAppenderPDO.php (line 47)

LoggerAppender
   |
   --LoggerAppenderPDO
Method Summary
LoggerAppenderPDO __construct ([string $name = ''])
void __destruct ()
boolean activateOptions ()
void append (LoggerLoggingEvent $event)
void close ()
void setCreateTable ( $flag)
void setDSN ( $dsn)
void setInsertPattern ($pattern $pattern)
void setInsertSql ($sql $sql)
void setPassword ( $password)
void setSql ( $sql)
void setTable ( $table)
void setUser ( $user)
Variables
Methods
Constructor __construct (line 99)

Constructor.

This apender doesn't require a layout.

  • access: public
LoggerAppenderPDO __construct ([string $name = ''])
  • string $name: appender name

Redefinition of:
LoggerAppender::__construct()
Constructor
Destructor __destruct (line 104)
  • access: public
void __destruct ()
activateOptions (line 115)

Setup db connection.

Based on defined options, this method connects to db defined in $dsn and creates a $table table if $createTable is true.

  • return: true if all ok.
  • throws: a PDOException if the attempt to connect to the requested database fails.
  • access: public
boolean activateOptions ()

Redefinition of:
LoggerAppender::activateOptions()
Derived appenders should override this method if option structure requires it.
append (line 177)

Appends a new event to the database.

  • throws: LoggerException If the pattern conversion or the INSERT statement fails.
  • access: public
void append (LoggerLoggingEvent $event)

Redefinition of:
LoggerAppender::append()
Subclasses of LoggerAppender should implement this method to perform actual logging.
close (line 199)

Closes the connection to the logging database

  • access: public
void close ()

Redefinition of:
LoggerAppender::close()
Release any resources allocated.
getDatabaseHandle (line 292)

Sometimes databases allow only one connection to themselves in one thread.

SQLite has this behaviour. In that case this handle is needed if the database must be checked for events.

  • access: public
PDO getDatabaseHandle ()
setCreateTable (line 228)

Indicator if the logging table should be created on startup, if its not existing.

  • access: public
void setCreateTable ( $flag)
  • $flag
setDSN (line 281)

Sets the DSN string for this connection. In case of

SQLite it could look like this: 'sqlite:appenders/pdotest.sqlite'

  • access: public
void setDSN ( $dsn)
  • $dsn
setInsertPattern (line 265)

Sets the LoggerLayoutPattern format strings for $insertSql.

It's not necessary to change this except you have customized logging.

  • access: public
void setInsertPattern ($pattern $pattern)
  • $pattern $pattern: Comma separated format strings like "%p,%m,%C"
setInsertSql (line 254)

Sets the SQL INSERT string to use with $insertPattern.

  • access: public
void setInsertSql ($sql $sql)
  • $sql $sql: A complete INSERT INTO query with "?" that gets replaced.
setPassword (line 220)

Sets the password for this connection.

Defaults to ''

  • access: public
void setPassword ( $password)
  • $password
setSql (line 245)

Sets the SQL string into which the event should be transformed.

Defaults to:

INSERT INTO $this->table ( timestamp, logger, level, message, thread, file, line) VALUES ('%d','%c','%p','%m','%t','%F','%L')

It's not necessary to change this except you have customized logging'

void setSql ( $sql)
  • $sql
setTable (line 273)

Sets the tablename to which this appender should log.

Defaults to log4php_log

  • access: public
void setTable ( $table)
  • $table
setUser (line 212)

Sets the username for this connection.

Defaults to ''

  • access: public
void setUser ( $user)
  • $user

Inherited Methods

Inherited From LoggerAppender

LoggerAppender::__construct()
LoggerAppender::activateOptions()
LoggerAppender::addFilter()
LoggerAppender::append()
LoggerAppender::clearFilters()
LoggerAppender::close()
LoggerAppender::doAppend()
LoggerAppender::getFilter()
LoggerAppender::getFirstFilter()
LoggerAppender::getLayout()
LoggerAppender::getName()
LoggerAppender::getThreshold()
LoggerAppender::isAsSevereAsThreshold()
LoggerAppender::requiresLayout()
LoggerAppender::setLayout()
LoggerAppender::setName()
LoggerAppender::setThreshold()

Documentation generated on Fri, 27 Nov 2009 07:44:47 +0100 by phpDocumentor 1.4.3