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)

LoggerConfigurable
   |
   --LoggerAppender
      |
      --LoggerAppenderPDO
Variable Summary
boolean $canAppend
string $createTable
PDO $db
string $dsn
string $insertSql
string $password
PDOStatement $preparedInsert
string $sql
string $table
string $user
Method Summary
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
boolean $canAppend = true (line 114)

Set in activateOptions() and later used in append() to check if all conditions to append are true.

  • access: protected
string $createTable = true (line 53)

Create the log table if it does not exists (optional).

  • access: protected
PDO $db = null (line 102)

The PDO instance.

  • access: protected
string $dsn (line 71)

DSN string for enabling a connection.

  • access: protected
string $insertPattern = "%d,%c,%p,%m,%t,%F,%L" (line 90)

A comma separated list of LoggerPatternLayout format strings that replace the "?" in $sql.

  • access: protected
string $insertSql = "INSERT INTO __TABLE__ (timestamp, logger, level, message, thread, file, line) VALUES (?,?,?,?,?,?,?)" (line 84)

Can be set to a complete insert statement with ? that are replaced using insertPattern.

  • access: protected
string $password (line 65)

Database password

  • access: protected
PDOStatement $preparedInsert (line 108)

Prepared statement for the INSERT INTO query.

  • access: protected
mixed $requiresLayout = false (line 119)

This appender does not require a layout.

  • access: protected

Redefinition of:
LoggerAppender::$requiresLayout
Set to true if the appender requires a layout.
string $sql (line 78)

A LoggerPatternLayout string used to format a valid insert query.

string $table = 'log4php_log' (line 96)

Table name to write events. Used only for CREATE TABLE if $createTable is true.

  • access: protected
string $user (line 59)

Database user name.

  • access: protected

Inherited Variables

Inherited from LoggerAppender

LoggerAppender::$closed
LoggerAppender::$filter
LoggerAppender::$layout
LoggerAppender::$name
LoggerAppender::$threshold
Methods
activateOptions (line 128)

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()
Prepares the appender for logging.
append (line 191)

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()
Forwards the logging event to the destination.
close (line 213)

Closes the connection to the logging database

  • access: public
void close ()

Redefinition of:
LoggerAppender::close()
Releases any resources allocated by the appender.
getDatabaseHandle (line 306)

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 242)

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

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

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 279)

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 268)

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 234)

Sets the password for this connection.

Defaults to ''

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

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 287)

Sets the tablename to which this appender should log.

Defaults to log4php_log

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

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::getDefaultLayout()
LoggerAppender::getFilter()
LoggerAppender::getFirstFilter()
LoggerAppender::getLayout()
LoggerAppender::getName()
LoggerAppender::getThreshold()
LoggerAppender::isAsSevereAsThreshold()
LoggerAppender::requiresLayout()
LoggerAppender::setLayout()
LoggerAppender::setName()
LoggerAppender::setThreshold()
LoggerAppender::warn()

Inherited From LoggerConfigurable

LoggerConfigurable::setBoolean()
LoggerConfigurable::setFileSize()
LoggerConfigurable::setInteger()
LoggerConfigurable::setLevel()
LoggerConfigurable::setNumeric()
LoggerConfigurable::setPositiveInteger()
LoggerConfigurable::setString()
LoggerConfigurable::warn()

Documentation generated on Sat, 18 Feb 2012 22:32:22 +0000 by phpDocumentor 1.4.3