1

It seems that the LoggerAppenderMongoDB of log4php does not require a layout, and it dose not even use any layout at least by default even if you specify a layout in the config.xml file.

Dose anybody know that how to force log4php LoggerAppenderMongoDB to use the specified layout in the config.xml?

my config.xml looks like:

<appender name="myConsoleAppender" class="LoggerAppenderConsole" />

<appender name="myFileAppender" class="LoggerAppenderFile">
    <layout class="LoggerLayoutPattern">
        <param name="conversionPattern" value="%date  %server{REMOTE_ADDR}:%server{REMOTE_PORT} [%logger] %message%newline" />
    </layout>
    <param name="file" value="myLog.log" />
</appender>

<appender name="myMongoDBAppender" class="LoggerAppenderMongoDB">

    <layout class="LoggerLayoutPattern">
        <param name="conversionPattern" value="%date  %server{REMOTE_ADDR}:%server{REMOTE_PORT} [%logger] %message%newline" />
    </layout>

    <param name="host" value="mongodb://xxxxx" />
    <param name="port" value="xxxx" />
    <param name="databaseName" value="xxxxx" />
    <param name="collectionName" value="xxxx" />
</appender>

<logger name="myLogger">
    <appender_ref ref="myMongoDBAppender" />
</logger>

<root>
    <appender_ref ref="myFileAppender" />
</root>

Now the logs going to the file are using the specified layout, but the ones going to MongoDB is not.

1 Answers1

0

The MongoDB appender does not offer to use a layout because the logged event is written to Mongo in a structured way: You'll be able to fetch distinct information without the need to merge them all together into one line.

Now I am not saying that the appender does retain every possible info, because the appender only fills the document with these info: 'timestamp', 'level', 'thread', 'message', 'loggerName', 'fileName', 'method', 'lineNumber', 'className', 'exception'.

What seems to be missing is the info put into MDC or NDC (mapped diagnostic context, nested diagnostic context), which is a very nice feature if you use and need it. What is also missing is what you tried to add into the layout: The remote IP address and port from $_SERVER. I'd suggest you make a feature request to add these infos.

In the meantime, you might be able to extend the existing appender to fit your own needs.

Sven
  • 69,403
  • 10
  • 107
  • 109