Writing to the Correct Log

写入正确的日志


Let's start with the basics first. In order to write into the log files correctly, we should make use of the LogUtil (Source code: https://github.com/jogetworkflow/jw-community/blob/6.0-SNAPSHOT/wflow-commons/src/main/java/org/joget/commons/util/LogUtil.java) utility class.

We should not use the following to print out log in writing our own plugins.


让我们先从基础开始。为了正确写入日志文件,我们应该使用LogUtil (源代码: https://github.com/jogetworkflow/jw-community/blob/6.0-SNAPSHOT/wflow-commons/src/main/java/org/joget/commons/util/LogUtil.java)实用程序类。


在编写我们自己的插件时,我们不应使用以下内容打印日志。

เริ่มจากพื้นฐานกันก่อน ในการเขียนไฟล์บันทึกอย่างถูกต้องเราควรใช้ LogUtil (ซอร์สโค้ด: https://github.com/jogetworkflow/jw-community/blob/6.0-SNAPSHOT/wflow-commons/src/main/java/org/joget/commons/util/LogUtil.java) คลาสยูทิลิตี้ เมื่อเขียนปลั๊กอินของเราเราไม่ควรใช้สิ่งต่อไปนี้เพื่อพิมพ์บันทึก

System.out.println("Execution is successful");

This is because this line of message would appear in catalina.out but not in the default Joget's log file, joget.log.

Instead, we should make use of these methods provided by LogUtil. Check out the some sample in the codes used by Email Tool (Source code: https://github.com/jogetworkflow/jw-community/blob/6.0-SNAPSHOT/wflow-core/src/main/java/org/joget/apps/app/lib/EmailTool.java#L227)

这是因为此消息行将出现在catalina.out中,而不出现在默认的Joget日志文件joget.log中。

相反,我们应该利用LogUtil提供的这些方法。在Email Tool使用的代码中查看一些示例 (源代码: https://github.com/jogetworkflow/jw-community/blob/6.0-SNAPSHOT/wflow-core/src/main/java/org/joget/apps/app/lib/EmailTool.java#L227)

นี่เป็นเพราะบรรทัดข้อความนี้จะปรากฏใน catalina.out แทนไฟล์บันทึก Joget เริ่มต้น joget.log แต่เราควรใช้ประโยชน์จากวิธีการเหล่านี้โดย LogUtil ดูตัวอย่างในรหัสที่ใช้โดยเครื่องมืออีเมล (ซอร์สโค้ด: https://github.com/jogetworkflow/jw-community/blob/6.0-SNAPSHOT/wflow-core/src/main/java/org/joget/apps/app/lib/EmailTool.java#L227)

LogUtil.info(EmailTool.class.getName(), "EmailTool: Sending email from=" + email.getFromAddress().toString() + ", to=" + to + "cc=" + cc + ", bcc=" + bcc + ", subject=" + email.getSubject());
LogUtil.info(EmailTool.class.getName(), "EmailTool: Sending email completed for subject=" + email.getSubject());
LogUtil.error(EmailTool.class.getName(), ex, "");

Separate Logs of Different Origin/Plugins into Different Log Files

将不同来源/插件的日志分为不同的日志文件

You may have already noticed that by default, we have log file named as email.log as Email Tool and related plugins are writing into this specific file. We may also consider this approach in breaking down the number of lines being written into a single log file for better troubleshooting.


您可能已经注意到默认情况下,由于电子邮件工具和相关插件正在写入此特定文件,因此我们将日志文件命名为email.log。我们还可以考虑使用这种方法来分解写入单个日志文件的行数,以更好地进行故障排除。

导航到“ [JogetFolder] \ apache-tomcat-8.5.23 \ webapps \ jw \ WEB-INF \ classes \ log4j.properties”配置文件,并检查R2标签的使用,以了解EmailToolUserNotificationAuditTrailExportFormEmailTool如何写入email.log文件。

คุณอาจสังเกตเห็นแล้วว่าโดยค่าเริ่มต้นเรามีไฟล์บันทึกชื่อเป็น email.log เป็นเครื่องมืออีเมลและปลั๊กอินที่เกี่ยวข้องกำลังเขียนลงในไฟล์นี้โดยเฉพาะ นอกจากนี้เรายังสามารถพิจารณาใช้วิธีนี้เพื่อแยกจำนวนบรรทัดที่เขียนลงในไฟล์บันทึกเดียวเพื่อการแก้ไขปัญหาที่ดี นำทางไปยังไฟล์กำหนดค่า "[JogetFolder] \ apache-tomcat-8.5.23 \ webapps \ jw \ WEB-INF \ คลาส \ log4j.properties" และตรวจสอบการใช้แท็ก R2 เพื่อดูว่า EmailTool, UserNotificationAuditTrail และ ExportFormEmailTool เป็นอย่างไร แล้วเขียนลงในไฟล์ email.log


Navigate to the "[JogetFolder]\apache-tomcat-8.5.72\webapps\jw\WEB-INF\classes\log4j2.xml" configuration file and check out the use of EMAIL rolling files to see how EmailTool, UserNotificationAuditTrail, and ExportFormEmailTool are writing into email.log file.


        <RollingFile
            name="EMAIL"
            fileName="${LOGDIR}/email.log"
            filePattern="${LOGDIR}/email.log.%d{yyyyMMdd}.gz"
            ignoreExceptions="false">
            <PatternLayout>
                <Pattern>%-5p %d{dd MMM yyyy HH:mm:ss} %-50c - %m%throwable{0}%n</Pattern>
            </PatternLayout>
            <Policies>
                <OnStartupTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="10MB" />
                <TimeBasedTriggeringPolicy interval="1"/>
            </Policies>
            <DefaultRolloverStrategy max="5" />
        </RollingFile>   


        <!-- Email log file -->
        <Logger name="org.joget.apps.app.lib.EmailTool" level="debug" additivity="true">
            <AppenderRef ref="EMAIL"/>
        </Logger>
        <Logger name="org.joget.apps.app.lib.UserNotificationAuditTrail" level="debug" additivity="true">
            <AppenderRef ref="EMAIL"/>
        </Logger>
        <Logger name="org.joget.plugin.enterprise.ExportFormEmailTool" level="debug" additivity="true">
            <AppenderRef ref="EMAIL"/>
        </Logger>


Navigate to the "[JogetFolder]\apache-tomcat-8.5.23\webapps\jw\WEB-INF\classes\log4j.properties" configuration file and check out the use of R2 tag to see how EmailTool, UserNotificationAuditTrail, and ExportFormEmailTool are writing into email.log file.


log4j.logger.org.joget.apps.app.lib.EmailTool=DEBUG, R2
log4j.logger.org.joget.apps.app.lib.UserNotificationAuditTrail=DEBUG, R2
log4j.logger.org.joget.plugin.enterprise.ExportFormEmailTool=DEBUG, R2

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-5p %d{dd MMM yyyy HH:mm:ss} %c %x - %m%n

# R is set to be DailyRollingFileAppender
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=${catalina.home}/logs/joget.log
log4j.appender.R.DatePattern='.'yyyyMMdd
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%-5p %d{dd MMM yyyy HH:mm:ss} %c %x - %m%n

# R2 is set to be DailyRollingFileAppender
log4j.appender.R2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R2.File=${catalina.home}/logs/email.log
log4j.appender.R2.DatePattern='.'yyyyMMdd
log4j.appender.R2.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.R2.layout.ConversionPattern=%-5p %d{dd MMM yyyy HH:mm:ss} %-50c - %m%throwable{0}%n


Identifying App Origin in Log Files

在日志文件中识别应用程序来源

In the section above, we talked about using LogUtil to write into the log files and how to write into separate files too. When we have too many apps running in the same copy of Joget, sometimes it is trace certain line of messages to the origin of Joget apps that trigger them.

For example, let's look at these log messages.

在上一节中,我们讨论了使用LogUtil写入日志文件以及如何写入单独的文件。当我们在同一个Joget副本中运行太多应用程序时,有时会将某些消息行追溯到触发它们的Joget应用程序的来源。

例如,让我们看一下这些日志消息。

ในส่วนด้านบนเราได้พูดคุยเกี่ยวกับการใช้ LogUtil เพื่อเขียนลงในล็อกไฟล์และวิธีเขียนลงในไฟล์แยกกันด้วย เมื่อเรามีแอพจำนวนมากที่ทำงานอยู่ใน Joget เดียวกันบางครั้งก็มีการติดตามข้อความบางข้อความถึงต้นกำเนิดของแอพ Joget ที่เรียกใช้งาน ตัวอย่างเช่นลองดูที่ข้อความบันทึกเหล่านี้

ERROR 17 Jun 2019 17:29:39 org.joget.apps.app.lib.EmailTool  - org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.outlook.comtest:587
org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.outlook.comtest:587
	at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1421)
	at org.apache.commons.mail.Email.send(Email.java:1448)
	at org.joget.apps.app.lib.EmailTool$1.run(EmailTool.java:239)
	at java.lang.Thread.run(Thread.java:748)
	at org.joget.commons.util.PluginThread.run(PluginThread.java:22)

There is no way that we can tell from which Joget app the EmailTool is triggered. However, in a Process Tool's execute method, we can obtain the appDef object which contains the Joget app information. Check out the sample code below.

我们无法确定从哪个Joget应用程序触发了EmailTool。但是,在Process Tool的execute方法中,我们可以获得包含Joget应用程序信息的appDef对象。

查看下面的示例代码。

ไม่มีวิธีที่เราสามารถบอกได้ว่าแอพ Joget ใดที่ EmailTool ทำงาน อย่างไรก็ตามในวิธีการดำเนินการของเครื่องมือประมวลผลเราสามารถรับวัตถุ appDef ซึ่งมีข้อมูลแอป Joget ลองดูโค้ดตัวอย่างด้านล่าง

public Object execute(Map properties) {
	
	AppDefinition appDef = (AppDefinition) properties.get("appDef");
	String appInfoAndMessage = appDef.toString() + "- Something happened";
	LogUtil.error(EmailTool.class.getName(), ex, appInfoAndMessage);
	
}

This way, we would be able to trace to the app that triggers and writes the line of message in the log file.

这样,我们将能够跟踪到触发并在日志文件中写入消息行的应用程序。

ด้วยวิธีนี้เราจะสามารถติดตามไปยังแอปพลิเคชันที่เรียกใช้และเขียนบรรทัดข้อความในไฟล์บันทึก

The object "appDef" is available in the following type of plugins.

以下类型的插件中提供了对象“ appDef”。

  • Form Post Submission Processing Tool

  • Process Tool

In other plugin types, we can try to obtain the App Definition object by using the following codes.

在其他插件类型中,我们可以尝试使用以下代码来获取App Definition对象。
import org.joget.apps.app.service.AppUtil;

AppDefinition appDef = AppUtil.getCurrentAppDefinition();


Large catalina.out File

大catalina.out文件

We can consider to LogRotate the log files. Please see the following links:-

我们可以考虑对日志文件进行LogRotate。请查看以下链接:

เราสามารถพิจารณา LogRotate ไฟล์บันทึก โปรดดูลิงค์ต่อไปนี้: -

As for joget.log, we are already using Log4J for rotation as seen in the log4j.properties file snippet above.

至于joget.log,我们已经使用Log4J进行旋转了,如上面的log4j.properties文件片段所示。

สำหรับ joget.log เราใช้ Log4J สำหรับการหมุนดังที่แสดงในตัวอย่างไฟล์ log4j.properties ด้านบน