Your plugin probably hasn't been loaded. You need to either add the package
your plugin is part of to the packages attribute of the configuration element
(see
http://logging.apache.org/log4j/2.x/manual/configuration.html#ConfigurationSyntax)
or follow the Plugin Preloading instructions at
http://logging.apache.org/log4j/2.x/manual/plugins.html
Ralph
On Jan 7, 2013, at 8:42 AM, Awasthi, Anand wrote:
> Hi,
>
> I have written custom JDBCAppender to write log messages to database. I have
> Hello World type program to test JDBCAppender. When I run the program, I get
> following error:
>
>
> 2013-01-07 11:37:28,753 ERROR appenders contains an invalid element or
> attribute "JDBCAppender"
>
>
> Here is my log4j2.xml :
>
>
> <?xml version="1.0" encoding="UTF-8"?>
> <configuration status="trace">
> <appenders>
> <JDBCAppender name="jdbc" driver="com.mysql.jdbc.Driver"
> url="jdbc:localhost:testdb" userName="root" password=""/>
> </appenders>
> <loggers>
> <root level="trace">
> <appender-ref ref="jdbc"/>
> </root>
> </loggers>
> </configuration>
>
>
>
> And JDBCAppender code is below:
>
> @Plugin(name = "JDBCAppender", type = "Core", elementType = "appender",
> printObject = true)
> public final class JDBCAppender extends AbstractAppender {
>
> private final Connection con;
> private final String sql;
>
> @SuppressWarnings("unchecked")
> private JDBCAppender(String name, Filter filter,
> @SuppressWarnings("rawtypes") Layout layout, Connection con, String sql,
> boolean handleExceptions) {
> super(name, filter, layout, handleExceptions);
> this.con = con;
> this.sql = sql;
> }
>
> public void append(LogEvent event) {
> try {
>
> Statement stmt = con.createStatement();
> stmt.execute(sql);
> con.commit();
> con.close();
> } catch (Exception ex) {
> throw new AppenderRuntimeException(ex);
> }
> }
>
> @PluginFactory
> public static JDBCAppender createAppender(@PluginAttr("url") String
> databaseURL,
> @PluginAttr("driver")
> String driverName,
> @PluginAttr("userName")
> String databaseUser,
> @PluginAttr("password")
> String databasePassword,
> @PluginElement("sql")
> String sql,
>
> @SuppressWarnings("rawtypes") @PluginElement("layout") Layout layout,
> @PluginElement("filter")
> Filter filter,
>
> @PluginAttr("suppressExceptions") String suppress) {
>
> String name = "JDBC Connection Info " + databaseURL;
> boolean handleExceptions = suppress == null ? true :
> Boolean.valueOf(suppress);
> Connection con = null;
> if (!DriverManager.getDrivers().hasMoreElements())
> setDriver(driverName);
> if (con == null) {
> try {
> con =
> DriverManager.getConnection(databaseURL, databaseUser,
> databasePassword);
> } catch (SQLException e) {
> // TODO Auto-generated catch
> block
> e.printStackTrace();
> }
> }
>
> if (con == null) {
> return null;
> }
> if (layout == null) {
> layout = SerializedLayout.createLayout();
> }
> return new JDBCAppender(name, filter, layout, con, sql,
> handleExceptions);
> }
>
> public static void setDriver(String driverClass) {
> try {
> Class.forName(driverClass);
> } catch (Exception e) {
>
> }
> }
>
> }
>
> Is there any other API changes or configuration needed to get Customer
> Appender work ?
>
>
>
> Thanks
> Anand
>
>
>