log4j2 пользовательский jdbcappender не захватывает два разных значения для переменной? - PullRequest
3 голосов
/ 06 февраля 2020

Ниже приведен пользовательский код приложения jdb c:

public void append(LogEvent arg0) {
        String flagg = "0";
        if(arg0.getLevel().toString().equalsIgnoreCase("ERROR")){
            flagg = "0";
            System.out.println("flagg : "+flagg);
        }
        else{
            flagg="-1";
            System.out.println("flagg : "+flagg);
        }

        MDC.put("flagg",flagg );
        System.out.println("flagg : "+ flagg);
        MDC.put("error_reason", "error");
    }

Вывод на консоль правильный:

    2020-02-08 12:57:11,735  INFO custom1.CustomAppenderApplication - info
flagg : -1
flagg : -1
2020-02-08 12:57:11,735 http-nio-8080-exec-4 DEBUG Loading driver class org.postgresql.Driver
2020-02-08 12:57:11,735 http-nio-8080-exec-4 DEBUG DriverManagerConnectionSource getting connection for 'jdbc:postgresql://127.0.0.1:5432/postgres'
2020-02-08 12:57:12,021 http-nio-8080-exec-4 DEBUG DriverManagerConnectionSource acquired connection for 'jdbc:postgresql://127.0.0.1:5432/postgres': org.postgresql.jdbc.PgConnection@65ab2959 (org.postgresql.jdbc.PgConnection@65ab2959)
2020-02-08 12:57:12,021 http-nio-8080-exec-4 DEBUG Preparing SQL: INSERT INTO ankita (timestamp,formatted_messege,session_id,graphname,level_string,flagg,error_reason) VALUES (?,?,?,?,?,?,?)
2020-02-08 12:57:12,022 http-nio-8080-exec-4 DEBUG Executing batch PreparedStatement INSERT INTO ankita (timestamp,formatted_messege,session_id,graphname,level_string,flagg,error_reason) VALUES ('2020-02-08 12:57:11.734+05:30','info','null','null','INFO','-1','error')
2020-02-08 12:57:12,025 http-nio-8080-exec-4 DEBUG Committing Connection org.postgresql.jdbc.PgConnection@65ab2959
2020-02-08 12:57:12,027 http-nio-8080-exec-4 DEBUG Closing PreparedStatement INSERT INTO ankita (timestamp,formatted_messege,session_id,graphname,level_string,flagg,error_reason) VALUES ('2020-02-08 12:57:11.734+05:30','info','null','null','INFO','-1','error')
2020-02-08 12:57:12,027 http-nio-8080-exec-4 DEBUG Closing Connection org.postgresql.jdbc.PgConnection@65ab2959
2020-Feb-08 12:57:12 PM  ERROR custom1.CustomAppenderApplication - error 
flagg : 0
flagg : 0
2020-02-08 12:57:12,027 http-nio-8080-exec-4 DEBUG Loading driver class org.postgresql.Driver
2020-02-08 12:57:12,028 http-nio-8080-exec-4 DEBUG DriverManagerConnectionSource getting connection for 'jdbc:postgresql://127.0.0.1:5432/postgres'
2020-02-08 12:57:12,176 http-nio-8080-exec-4 DEBUG DriverManagerConnectionSource acquired connection for 'jdbc:postgresql://127.0.0.1:5432/postgres': org.postgresql.jdbc.PgConnection@68f49e2f (org.postgresql.jdbc.PgConnection@68f49e2f)
2020-02-08 12:57:12,176 http-nio-8080-exec-4 DEBUG Preparing SQL: INSERT INTO ankita (timestamp,formatted_messege,session_id,graphname,level_string,flagg,error_reason) VALUES (?,?,?,?,?,?,?)
2020-02-08 12:57:12,176 http-nio-8080-exec-4 DEBUG Executing batch PreparedStatement INSERT INTO ankita (timestamp,formatted_messege,session_id,graphname,level_string,flagg,error_reason) VALUES ('2020-02-08 12:57:12.027+05:30','error','kuchh v','main v','ERROR','-1','error')
2020-02-08 12:57:12,180 http-nio-8080-exec-4 DEBUG Committing Connection org.postgresql.jdbc.PgConnection@68f49e2f
2020-02-08 12:57:12,182 http-nio-8080-exec-4 DEBUG Closing PreparedStatement INSERT INTO ankita (timestamp,formatted_messege,session_id,graphname,level_string,flagg,error_reason) VALUES ('2020-02-08 12:57:12.027+05:30','error','kuchh v','main v','ERROR','-1','error')
2020-02-08 12:57:12,183 http-nio-8080-exec-4 DEBUG Closing Connection org.postgresql.jdbc.PgConnection@68f49e2f

Но проблема в postgresql flagg равен -1 даже за ошибку.

Значение flagg всегда равно -1, но я протестировал другое значение жесткого кодирования в MD C .put, и оно отлично работает.

Как установить соответствующее значение для flagg в postgresql , Ниже мой log4j2. xml

<JDBC name="databaseAppender" tableName="...">

            <DriverManager connectionString="jdbc:postgresql://127.0.0.1:5432/postgres" 
            driverClassName="org.postgresql.Driver"
            username="postgres" 
            password="..." />

            <Column name="flagg" pattern="%X{flagg}" isUnicode="false" />
            <Column name="error_reason" pattern="%X{error_reason}" isUnicode="false"/>
      </JDBC>

Спасибо.

...