How to write to log files from a Zimbra Extension

In you can find some examples of how to write to the
Zimbra log files. The most common ones are:"this is an info message that will show up in /opt/zimbra/log/mailbox.log");
ZimbraLog.extensions.error("this is an error message that will show up in /opt/zimbra/log/mailbox.log");

To see the logging in action run a tail on the log file as user root and
visit the extension page in your browser

tail -f /opt/zimbra/log/mailbox.log

The log will look as follows:

2022-10-26 11:12:25,174 INFO  [qtp48914743-18:] [] extensions - this is an info message that will show up in /opt/zimbra/log/mailbox.log
2022-10-26 11:12:25,174 ERROR [qtp48914743-18:] [] extensions - this is an error message that will show up in /opt/zimbra/log/mailbox.log

This is an example of a log message that contains variables:"Some error happened : %s for : %s", Long.toString(a), e.getMessage());

It will show in the log as follows:

2022-10-26 11:12:25,174 INFO  [qtp48914743-18:] [] extensions - Some error happened : 24567 for : / by zero

Different log levels exist to keep the size of the logging manageable in
a production environment. You can use the debug log level to
troubleshoot problems and/or log extensive amounts of data. By default
the log level in Zimbra is set so that INFO and ERROR level messages are
stored in the log and debug level messages are not stored.

To enable the storing of debug level messages for extensions append to
/opt/zimbra/conf/ and
/opt/zimbra/conf/ the following: = zimbra.extensions
logger.extensions.level = debug
logger.extensions.additivity = false
logger.extensions.appenderRef.LOGFILE.ref = mailboxFile

You will have to restart Zimbra using:

sudo su zimbra -
zmmailboxdctl restart

You can now use the following:

ZimbraLog.extensions.debug("this is a debug message that will show up in /opt/zimbra/log/mailbox.log if debug logging level is set for extensions");

This will show up in the /opt/zimbra/log/mailbox.log like this:

2022-10-26 11:12:25,174 DEBUG [qtp48914743-18:] [] extensions - this is a debug message that will show up in /opt/zimbra/log/mailbox.log if debug logging level is set for extensions

Other means of logging that you should avoid if possible:

System.out.println("This logs to /opt/zimbra/log/zmmailboxd.out, avoid using this");

This will literally only print the text of the message, but no date/time
stamp. So it can be used at development time, but in production it will
be hard to correlate the error message with other events logged by

printStackTrace() is very useful in diagnosing exceptions during
development time. Most of the exception you should deal with properly in
your code and that should avoid the need for calling printStackTrace() a
lot in production. So use it wisely. An example:

long a = 24567;
long b = 0;
try {
    long c = (a / b) * 100; //Cannot divide by zero
} catch (Exception e) {
    //printStackTrace() logs to /opt/zimbra/log/zmmailboxd.out, avoid using this

This will log around 80 lines of Java stack trace to
/opt/zimbra/log/zmmailboxd.out it looks like this:

java.lang.ArithmeticException: / by zero
    at com.example.mytest.Mytest.doGet(
    at com.zimbra.cs.extension.ExtensionDispatcherServlet.service(
    at javax.servlet.http.HttpServlet.service(
    at org.eclipse.jetty.servlet.ServletHolder.handle(
    at org.eclipse.jetty.servlet.ServletHandler$ChainEnd.doFilter(

, ,

2 Responses to How to write to log files from a Zimbra Extension

  1. Aio January 24, 2023 at 7:04 PM #

    hi, how can we enable audit log to be included on syslog or remote log server? like the log for deleted user or config changes.

Copyright © 2022 Zimbra, Inc. All rights reserved.

All information contained in this blog is intended for informational purposes only. Synacor, Inc. is not responsible or liable in any manner for the use or misuse of any technical content provided herein. No specific or implied warranty is provided in association with the information or application of the information provided herein, including, but not limited to, use, misuse or distribution of such information by any user. The user assumes any and all risk pertaining to the use or distribution in any form of any subject matter contained in this blog.

Legal Information | Privacy Policy | Do Not Sell My Personal Information | CCPA Disclosures