Documents for Excel, Java Edition Documentation
Features / Logging
In This Topic
    In This Topic

    GcExcel Java supports logging which allows you to capture logs and resolve issues by finding out their root cause. It uses Apache Commons Logging component to implement the logging system. You can refer Apache Commons Logging for more information. It allows you to capture four log levels, namely, debug, info, warn and error by using GcExel logging system. The priority order of these levels is:

    debug < info < warn < error

    You can configure which log level needs to be printed to the log file. After setting the log level, the logs with higher or equal priority are included in the printed logs while the logs with lower priority are ignored. For example, if you set the log level to "info", the info, warn and error logs are printed, while the "debug" logs are ignored. The logging information is supported for Json and Excel I/O and PDF exporting.

    Project Dependencies

    GcExcel Java needs commons-logging.jar library as a dependency to implement logging system. For maven or gradle projects, this library is included automatically with GcExcel. However, for other java projects, it needs to be included manually.

    Enable Logging System

    In GcExcel Java, the logging system is enabled by default and the default log level is "info".

    Configure Logging System

    The logs are printed on system console by default. In order to print the logs to a file or change the format of logs, you need to use a professional logging implementation library, such as log4j. The following steps explain how to use log4j2 in GcExcel Java:

    1. Include Jars in classpath

    1.1 For Java console project, download, unzip it and copy the following jars manually to the project.

    Add the above jars to buildpath

    1.2  For maven project, add below config in pom.xml:

    Copy Code

    1.3  For gradle project, add below config in build.gradle:

    Copy Code
    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.3'
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.3'
    compile group: 'org.apache.logging.log4j', name: 'log4j-jcl', version: '2.3'

    2. Add Configuration File

    2.1 For Java console project, add a log4j2.xml file under src folder of the project. The sample content of log4j2.xml file can be:

    Copy Code
    <?xml version="1.0" encoding="UTF-8"?>
        <Console name="STDOUT" target="SYSTEM_OUT">
          <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
        <Logger name="org.apache.fontbox" level="info"/>
        <Logger name="org.apache.pdfbox" level="info"/>
        <Logger name="org.apache.log4j.xml" level="info"/>
        <Root level="debug">
          <AppenderRef ref="STDOUT"/>

    2.2 For maven and gradle project, add above log4j2.xml file under folder src/main/resources. If the 'resources' folder does not exist by default, you can create a new 'resources' folder under src/main

    Launch Logging System

    The following sample code sets the format in which logs will be exported to PDF files. Run the application to see the debug logs printed on console and saved to PDF files.   

    Copy Code
    for (int i = 0; i < 5; i++) {
        final int id = i + 1;
        Thread thread = new Thread(new Runnable() {
            public void run() {
                // Create workbook
                IWorkbook workbook = new Workbook();
                // Set data
                IRange A1 = workbook.getActiveSheet().getRange("A1");
                A1.setValue("For log demo" + id + "!");
                // Export to PDF
      "logdemo" + id + ".pdf");
        }, "LogThread" + id);
    Note: You can also use third-party logging implementations as well as log4j, such as: slf4j, LogBack, Avalon LogKit etc.