0

In dynamic web application i'm unable to log messages with custom levels.

  1. i'm building a Java Dynamic Web project with (java8), wildfly11 server with log4j2 (version 2.12.1) logger for logging.
  2. in log4j2.xml i have created 3 custom levels.
  3. In java code i'm writing some custom logs but those are not even displayed in console and not even written in specified log file.
  4. log4j2.xml file is attached for reference.
  5. when i deploy the web project getting following error.

Could not index class META-INF/versions/9/module-info.class at /D:/JBOSS Server/wildfly-11.0.0.Final/standalone/deployments/asset-services.war/WEB-INF/lib/log4j-api-2.12.1.jar: java.lang.IllegalStateException: Unknown tag! pos=4 poolCount = 33 at org.jboss.jandex.Indexer.processConstantPool(Indexer.java:1417) at org.jboss.jandex.Indexer.index(Indexer.java:1451)

CODE:

<Configuration status="WARN">
    <CustomLevels>
        <CustomLevel name="AUDIT_INFO" intLevel="1" />
            <CustomLevel name="AUDIT_SUCCESS" intLevel="2" />
            <CustomLevel name="AUDIT_FALURE" intLevel="3" />
        </CustomLevels>
        <Appenders>
            <Console name="Console" target="SYSTEM_OUT">
                <PatternLayout pattern="%d [%t] %-5level %logger{36} - %msg%n"/>
            </Console>

            <RollingFile name="AuditLog" fileName="..\\standalone\\log\\ASSETAudit.log" filePattern="..\\standalone\\log\\ASSETAudit-%d-%i{yyyy-MM-dd}.log" ignoreExceptions="false" immediateFlush="true">
                <Jsonlayout complete="true" compact ="true" eventEol="true" objectMessageAsJsonObject="false" includeStacktrace="false">
                </Jsonlayout>
                <Policies>
                    <OnStartupTriggeringPolicy/>
                    <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
                </Policies>
            </RollingFile>

            <RollingFile name="TransactionLog" fileName="..\\standalone\\log\\ASSET.log" filePattern="..\\standalone\\log\\ASSET-%d-%i{yyyy-MM-dd}.log.gz" ignoreExceptions="false" immediateFlush="true">
                <PatternLayout pattern="%d [%t] %-5level %logger{36} - %msg%n" />
                <Policies>
                    <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
                    <SizeBasedTriggeringPolicy size="250MB"/>
                </Policies>
            </RollingFile>
        </Appenders>
        <Loggers>
            <Root level="INFO">
                <AppenderRef ref="Console"/>
                <AppenderRef ref="TransactionLog" level="INFO" />
                <AppenderRef ref="AuditLog" level="AUDIT_FALURE" />
            </Root>
        </Loggers>
    </Configuration>

audit logs with custom level audit_info,audit_success,audit_failure. should be written to assetaudit.log file

Giri_Raj
  • 31
  • 5
  • If i change the AppenderRef value of "Auditlog" from "AUDIT_FALURE" to "INFO", all the info messages are written to the assetaudit.log file. – Giri_Raj Aug 23 '19 at 09:54
  • In my opinion, unless you have a very good reason for using custom levels it's generally a better practice to use a different logger name instead. E.g. `Logger log = LogManager.getLogger("MyAuditLogger");` Then configure log4j2 such that your special audit logger sends its messages to your audit log file. Other options include using [Markers](https://logging.apache.org/log4j/2.x/manual/markers.html) or [RoutingAppender](https://logging.apache.org/log4j/2.x/manual/appenders.html#RoutingAppender) – D.B. Aug 23 '19 at 14:15
  • @D.B. the reason for having the custom levels is that i.\n 1. I want to display all the audit log events to user in GUI. 2. Maintaining a separate log file for audit will be easier to read the data in JSON format and display in web page. – Giri_Raj Aug 24 '19 at 03:58
  • @D.B. is it possible to achieve the audit functionality using Markers.? if so please share some info/examples. – Giri_Raj Aug 24 '19 at 04:02
  • Please see my [answer to another question](https://stackoverflow.com/a/50803857/3284624) – D.B. Aug 24 '19 at 05:34
  • @D.B. This worked fine. Thanks a lot.:) – Giri_Raj Aug 24 '19 at 05:52

0 Answers0