{
    "mode": "perldoc",
    "parameter": "Log::Log4perl::Appender::DBI",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Log%3A%3ALog4perl%3A%3AAppender%3A%3ADBI/json",
    "generated": "2026-06-09T18:06:08Z",
    "synopsis": "my $config = q{\nlog4j.category = WARN, DBAppndr\nlog4j.appender.DBAppndr             = Log::Log4perl::Appender::DBI\nlog4j.appender.DBAppndr.datasource  = DBI:CSV:fdir=t/tmp\nlog4j.appender.DBAppndr.username    = bobjones\nlog4j.appender.DBAppndr.password    = 12345\nlog4j.appender.DBAppndr.sql         = \\\ninsert into log4perltest           \\\n(loglevel, custid, category, message, ipaddr) \\\nvalues (?,?,?,?,?)\nlog4j.appender.DBAppndr.params.1 = %p\n#2 is custid from the log() call\nlog4j.appender.DBAppndr.params.3 = %c\n#4 is the message from log()\n#5 is ipaddr from log()\nlog4j.appender.DBAppndr.usePreparedStmt = 1\n#--or--\nlog4j.appender.DBAppndr.bufferSize = 2\n#just pass through the array of message items in the log statement\nlog4j.appender.DBAppndr.layout    = Log::Log4perl::Layout::NoopLayout\nlog4j.appender.DBAppndr.warpmessage = 0\n#driver attributes support\nlog4j.appender.DBAppndr.attrs.fencoding = utf8\n};\nLog::Log4perl::init ( \\$config ) ;\nmy $logger = Log::Log4perl->getlogger () ;\n$logger->warn( $custid, 'big problem!!', $ipaddr );",
    "sections": {
        "NAME": {
            "content": "Log::Log4perl::Appender::DBI - implements appending to a DB\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "my $config = q{\nlog4j.category = WARN, DBAppndr\nlog4j.appender.DBAppndr             = Log::Log4perl::Appender::DBI\nlog4j.appender.DBAppndr.datasource  = DBI:CSV:fdir=t/tmp\nlog4j.appender.DBAppndr.username    = bobjones\nlog4j.appender.DBAppndr.password    = 12345\nlog4j.appender.DBAppndr.sql         = \\\ninsert into log4perltest           \\\n(loglevel, custid, category, message, ipaddr) \\\nvalues (?,?,?,?,?)\nlog4j.appender.DBAppndr.params.1 = %p\n#2 is custid from the log() call\nlog4j.appender.DBAppndr.params.3 = %c\n#4 is the message from log()\n#5 is ipaddr from log()\n\nlog4j.appender.DBAppndr.usePreparedStmt = 1\n#--or--\nlog4j.appender.DBAppndr.bufferSize = 2\n\n#just pass through the array of message items in the log statement\nlog4j.appender.DBAppndr.layout    = Log::Log4perl::Layout::NoopLayout\nlog4j.appender.DBAppndr.warpmessage = 0\n\n#driver attributes support\nlog4j.appender.DBAppndr.attrs.fencoding = utf8\n};\n\nLog::Log4perl::init ( \\$config ) ;\n\nmy $logger = Log::Log4perl->getlogger () ;\n$logger->warn( $custid, 'big problem!!', $ipaddr );\n",
            "subsections": []
        },
        "CAVEAT": {
            "content": "This is a very young module and there are a lot of variations in setups with different databases\nand connection methods, so make sure you test thoroughly! Any feedback is welcome!\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This is a specialized Log::Dispatch object customized to work with log4perl and its abilities,\noriginally based on Log::Dispatch::DBI by Tatsuhiko Miyagawa but with heavy modifications.\n\nIt is an attempted compromise between what Log::Dispatch::DBI was doing and what log4j's\nJDBCAppender does. Note the log4j docs say the JDBCAppender \"is very likely to be completely\nreplaced in the future.\"\n\nThe simplest usage is this:\n\nlog4j.category = WARN, DBAppndr\nlog4j.appender.DBAppndr            = Log::Log4perl::Appender::DBI\nlog4j.appender.DBAppndr.datasource = DBI:CSV:fdir=t/tmp\nlog4j.appender.DBAppndr.username   = bobjones\nlog4j.appender.DBAppndr.password   = 12345\nlog4j.appender.DBAppndr.sql        = \\\nINSERT INTO logtbl                \\\n(loglevel, message)            \\\nVALUES ('%c','%m')\n\nlog4j.appender.DBAppndr.layout    = Log::Log4perl::Layout::PatternLayout\n\n\n$logger->fatal('fatal message');\n$logger->warn('warning message');\n\n===============================\n|FATAL|fatal message          |\n|WARN |warning message        |\n===============================\n\nBut the downsides to that usage are:\n\n*   You'd better be darn sure there are not quotes in your log message, or your insert could\nhave unforeseen consequences! This is a very insecure way to handle database inserts, using\nplace holders and bind values is much better, keep reading. (Note that the log4j docs warn\n\"Be careful of quotes in your messages!\") *.\n\n*   It's not terribly high-performance, a statement is created and executed for each log call.\n\n*   The only run-time parameter you get is the %m message, in reality you probably want to log\nspecific data in specific table columns.\n\nSo let's try using placeholders, and tell the logger to create a prepared statement handle at\nthe beginning and just reuse it (just like Log::Dispatch::DBI does)\n\nlog4j.appender.DBAppndr.sql = \\\nINSERT INTO logtbl \\\n(custid, loglevel, message) \\\nVALUES (?,?,?)\n\n#---------------------------------------------------\n#now the bind values:\n#1 is the custid\nlog4j.appender.DBAppndr.params.2 = %p\n#3 is the message\n#---------------------------------------------------\n\nlog4j.appender.DBAppndr.layout    = Log::Log4perl::Layout::NoopLayout\nlog4j.appender.DBAppndr.warpmessage = 0\n\nlog4j.appender.DBAppndr.usePreparedStmt = 1\n\n\n$logger->warn( 1234, 'warning message' );\n\nNow see how we're using the '?' placeholders in our statement? This means we don't have to worry\nabout messages that look like\n\ninvalid input: 1234';drop table custid;\n\nfubaring our database!\n\nNormally a list of things in the logging statement gets concatenated into a single string, but\nsetting \"warpmessage\" to 0 and using the NoopLayout means that in\n\n$logger->warn( 1234, 'warning message', 'bgates' );\n\nthe individual list values will still be available for the DBI appender later on. (If\n\"warpmessage\" is not set to 0, the default behavior is to join the list elements into a single\nstring. If PatternLayout or SimpleLayout are used, their attempt to \"render()\" your layout will\nresult in something like \"ARRAY(0x841d8dc)\" in your logs. More information on \"warpmessage\" is\nin Log::Log4perl::Appender.)\n\nIn your insert SQL you can mix up '?' placeholders with conversion specifiers (%c, %p, etc) as\nyou see fit--the logger will match the question marks to params you've defined in the config\nfile and populate the rest with values from your list. If there are more '?' placeholders than\nthere are values in your message, it will use undef for the rest. For instance,\n\nlog4j.appender.DBAppndr.sql =                 \\\ninsert into log4perltest                   \\\n(loglevel, message, datestr, subpoenaid)\\\nvalues (?,?,?,?)\nlog4j.appender.DBAppndr.params.1 = %p\nlog4j.appender.DBAppndr.params.3 = %d\n\nlog4j.appender.DBAppndr.warpmessage=0\n\n\n$logger->info('arrest him!', $subpoenaid);\n\nresults in the first '?' placeholder being bound to %p, the second to \"arrest him!\", the third\nto the date from \"%d\", and the fourth to your $subpoenaid. If you forget the $subpoenaid and\njust log\n\n$logger->info('arrest him!');\n\nthen you just get undef in the fourth column.\n\nIf the logger statement is also being handled by other non-DBI appenders, they will just join\nthe list into a string, joined with $Log::Log4perl::JOINMSGARRAYCHAR (default is an empty\nstring).\n\nAnd see the \"usePreparedStmt\"? That creates a statement handle when the logger object is created\nand just reuses it. That, however, may be problematic for long-running processes like\nwebservers, in which case you can use this parameter instead\n\nlog4j.appender.DBAppndr.bufferSize=2\n\nThis copies log4j's JDBCAppender's behavior, it saves up that many log statements and writes\nthem all out at once. If your INSERT statement uses only ? placeholders and no %x conversion\nspecifiers it should be quite efficient because the logger can re-use the same statement handle\nfor the inserts.\n\nIf the program ends while the buffer is only partly full, the DESTROY block should flush the\nremaining statements, if the DESTROY block runs of course.\n\n* *As I was writing this, Danko Mannhaupt was coming out with his improved log4j JDBCAppender\n(http://www.mannhaupt.com/danko/projects/) which overcomes many of the drawbacks of the original\nJDBCAppender.*\n",
            "subsections": []
        },
        "DESCRIPTION 2": {
            "content": "Or another way to say the same thing:\n\nThe idea is that if you're logging to a database table, you probably want specific parts of your\nlog information in certain columns. To this end, you pass an list to the log statement, like\n\n$logger->warn('big problem!!',$userid,$subpoenanr,$ipaddr);\n\nand the array members drop into the positions defined by the placeholders in your SQL statement.\nYou can also define information in the config file like\n\nlog4j.appender.DBAppndr.params.2 = %p\n\nin which case those numbered placeholders will be filled in with the specified values, and the\nrest of the placeholders will be filled in with the values from your log statement's array.\n",
            "subsections": []
        },
        "MISC PARAMETERS": {
            "content": "usePreparedStmt\nSee above.\n\nwarpmessage\nsee Log::Log4perl::Appender\n\nmaxcolsize\nIf you're used to just throwing debugging messages like huge stacktraces into your logger,\nsome databases (Sybase's DBD!!) may surprise you by choking on data size limitations.\nNormally, the data would just be truncated to fit in the column, but Sybases's DBD it turns\nout maxes out at 255 characters. Use this parameter in such a situation to truncate long\nmessages before they get to the INSERT statement.\n\nCHANGING DBH CONNECTIONS (POOLING)\nIf you want to get your dbh from some place in particular, like maybe a pool, subclass and\noverride init() and/or createstatement(), for instance\n\nsub init {\n; #no-op, no pooling at this level\n}\nsub createstatement {\nmy ($self, $stmt) = @;\n\n$stmt || croak \"Log4perl: sql not set in \".PACKAGE;\n\nreturn My::Connections->getConnection->prepare($stmt)\n|| croak \"Log4perl: DBI->prepare failed $DBI::errstr\\n$stmt\";\n}\n",
            "subsections": []
        },
        "LIFE OF CONNECTIONS": {
            "content": "If you're using \"log4j.appender.DBAppndr.usePreparedStmt\" this module creates an sth when it\nstarts and keeps it for the life of the program. For long-running processes (e.g. modperl),\nconnections might go stale, but if \"Log::Log4perl::Appender::DBI\" tries to write a message and\nfigures out that the DB connection is no longer working (using DBI's ping method), it will\nreconnect.\n\nThe reconnection process can be controlled by two parameters, \"reconnectattempts\" and\n\"reconnectsleep\". \"reconnectattempts\" specifies the number of reconnections attempts the DBI\nappender performs until it gives up and dies. \"reconnectsleep\" is the time between reconnection\nattempts, measured in seconds. \"reconnectattempts\" defaults to 1, \"reconnectsleep\" to 0.\n\nAlternatively, use \"Apache::DBI\" or \"Apache::DBI::Cache\" and read CHANGING DB CONNECTIONS above.\n\nNote that \"Log::Log4perl::Appender::DBI\" holds one connection open for every appender, which\nmight be too many.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "Log::Dispatch::DBI\n\nLog::Log4perl::JavaMap::JDBCAppender\n",
            "subsections": []
        },
        "LICENSE": {
            "content": "Copyright 2002-2013 by Mike Schilli <m@perlmeister.com> and Kevin Goess <cpan@goess.org>.\n\nThis library is free software; you can redistribute it and/or modify it under the same terms as\nPerl itself.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Please contribute patches to the project on Github:\n\nhttp://github.com/mschilli/log4perl\n\nSend bug reports or requests for enhancements to the authors via our\n\nMAILING LIST (questions, bug reports, suggestions/patches): log4perl-devel@lists.sourceforge.net\n\nAuthors (please contact them via the list above, not directly): Mike Schilli\n<m@perlmeister.com>, Kevin Goess <cpan@goess.org>\n\nContributors (in alphabetical order): Ateeq Altaf, Cory Bennett, Jens Berthold, Jeremy Bopp,\nHutton Davidson, Chris R. Donnelly, Matisse Enzer, Hugh Esco, Anthony Foiani, James FitzGibbon,\nCarl Franks, Dennis Gregorovic, Andy Grundman, Paul Harrington, Alexander Hartmaier David Hull,\nRobert Jacobson, Jason Kohles, Jeff Macdonald, Markus Peter, Brett Rann, Peter Rabbitson, Erik\nSelberg, Aaron Straup Cope, Lars Thegler, David Viner, Mac Yang.\n",
            "subsections": []
        }
    },
    "summary": "Log::Log4perl::Appender::DBI - implements appending to a DB",
    "flags": [],
    "examples": [],
    "see_also": []
}