JDBCAppender tableName from Main Arguments Lookup

log4j2 xml example mkyong
log4j 2 example
onstartuptriggeringpolicy log4j2
log4j2 async logger properties file
log4j2 levelrangefilter example
log4j jdbc appender
onstartuptriggeringpolicy log4j2 example
log4j gradle example

There is a JDBCAppender in my application to which I'd like to set the tableName parameter from a main argument lookup. Reason for this is that my database table may optionally have a prefix, so I'm reading this from a configuration file in my application and I'd like to apply it to the appender at runtime.

    <JDBC name="TaskLogJDBC" tableName="${main:1}LogEvents">
        <ConnectionFactory class="my.package.name.ConnectionFactory" method="getConnection"/>
        <Column name="created_on" isEventTimestamp="true"/>
        <Column name="lvl" pattern="%level"/>
        <Column name="logger" pattern="%logger"/>
        <Column name="message" pattern="%message"/>
        <Column name="throwable" pattern="%ex{full}"/>
    </JDBC>

This is what I use in Main.java to se the arguments:

MainMapLookup.setMainArguments(new String[] { "--prefix", tablesPrefix });

Unfortunately, this does not seem to work. The JDBCAppender keeps reading the tableName parameter verbatim as ${main:0}LogEvents instead of prefix_LogEvents. I traced it and I think the JDBCAppender configuration read happens before the MainMapLookup.setMainArguments() gets called. Is this a bug?

Environment variable lookup is an option, but I'd like to avoid it at this point. Are there any other options?

Try:

static{
    MainMapLookup.setMainArguments(new String[] { "--prefix", tablesPrefix });
}

[#LOG4J2-2757] JDBCAppender tableName from Main Arguments , Question. LOG4J2-2757JDBCAppender tableName from Main Arguments Lookup � Bug. LOG4J2-2756ClassLoaderContextSelector: WeakReference and GC. Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

As of version 2.13.0, you MUST call Log4j's MainMapLookup.setMainArguments() from your app's main() method or anywhere that array is available from your app.

Log4j 2 -, Log4j2 Levels, Log4j2 Appenders, Lookups, Filters, Layout, PatternLayout. Lookup; JNDI Lookup; JVM Input Argument Lookup (JMX); Main Arguments Lookup The main goal of JDBCAppender is to write Log events into a relational table name="databaseAppender" tableName="journaldev.logging"> < DataSource� Log4j is a simple and flexible logging framework. Logging equips the developer with detailed context for application failures. With log4j it is possible to enable logging at runtime without modifying the application binary. The log4j package is designed so that these statements can remain in shipped code without incurring a heavy performance cost. Log4j comes […]

EDIT: After some more trial and error, I found out that the issue comes from having declared a Logger in the Main class itself.

private static final Logger logger = LogManager.getLogger(Main.class.getName());

This loads the log4j2.xml including all JDBC appender parameters before MainMapLookup.setMainArguments() gets invoked. Removing the logger from above allows for the main arguments to get set.


Nothing that I tried worked, because it seems my log4j2.xml file gets loaded before my application's main() gets called. So, I ended up hacking the log4j2 JdbcDatabaseManager class by using reflection. This replaces the name of the table directly in the SQL statement.

    final var jdbcAppender = (JdbcAppender) loggerContext.getConfiguration().getAppender("AppenderName");
    final var jdbcManager = jdbcAppender.getManager();
    var field = jdbcManager.getClass().getDeclaredField("sqlStatement");
    field.setAccessible(true);
    String value = (String) field.get(jdbcManager);
    value = value.replace("__DUMMY_TABLE__", prefix + "TaskLog");
    field.set(jdbcManager, value);

The table name is defined as so in the log4j2.xml file:

    <JDBC name="AppenderName" tableName="__DUMMY_TABLE__">

It seems to have worked. Hope it's helpful to others.

java - 来自主要参数查找的JDBCAppender tableName, <Appenders> <JDBC name="JDBCAppender" tableName="logs"> <DataSource For example, you can configure a primary JDBCAppender, with a We've already seen an example of the date lookup in the RollingFileAppender configuration: 11:28:23.558 [main] NEW_LEVEL - Custom Level Message� Log4j2 is the updated version of the popular and influential log4j library, used extensively throughout the Java ecosystem for so many years. Version 2.x keeps all the logging features of its predecessor and builds on that foundation with some significant improvements, especially in the area of performance.

Log4j2 Example Tutorial, JdbcAppender for element JDBC org.apache.logging.log4j.core.config. ConfigurationException: Arguments given for element JDBC are invalid: field I tried this: <Appenders> <JDBC name="SQLServerAppender" tableName="dbo. I tried A <loadData encoding="UTF-8" file="src/main/resources/data/ configuration.tsv"� Some arguments in the catalog functions, such as the TableName argument in SQLTables, accept search patterns. These arguments accept search patterns if the SQL_ATTR_METADATA_ID statement attribute is set to SQL_FALSE; they are identifier arguments that do not accept a search pattern if this attribute is set to SQL_TRUE.

Log4J2: How It Works and How to Get the Most Out Of It, This class describes the usage of JdbcAppender.java. main. java. org. apache. log4j. Appender.java; BasicConfigurator.java; Category.java; Layout. JavaLookup.java; JmxRuntimeInputArgumentsLookup.java; JndiLookup.java; Log4jLookup.java getManager(managerName, bufferSize, connectionSource, tableName,� JDBCAppender. The main goal of JDBCAppender is to write Log events into a relational table through JDBC connections. We don’t consume much time explaining how you can optimize your connection pools as this tutorial isn’t intended for this purpose. But for sure you will get a full functional example that helps writing your log events into

log4j2 appender talking to sql server database - log4j2, gmail.com> > Hi Matt, I am able to add "tableName" attribute for JDBC AppenderComponentBuilder But not the remaining stuff like ConnectionFactory class� static <B extends JdbcAppender.Builder<B>> JdbcAppender: createAppender (String name, String ignore, Filter filter, ConnectionSource connectionSource, String bufferSize, String tableName, ColumnConfig[] columnConfigs)

Comments
  • Code only answers are rarely helpful. Please comment your code or expound on why this is the solution to the question.
  • Although a clever idea, this won't help. tablesPrefix gets loaded from an external file after the application starts. Additionally, it seems the appender config in log4j2.xml file is being read even before the main() method runs. This really ties my hands, I guess.