วิธีที่ง่ายที่สุดในการสร้างบันทึกใน Java โดยใช้ SimpleFormatter และ XMLFormatter ของ java.util.logging
เผยแพร่แล้ว: 2020-05-26
Java มาพร้อมกับยูทิลิตี้มากมาย ในบทช่วยสอนนี้ เราจะพูดถึงวิธีที่ง่ายที่สุดในการสร้างไฟล์บันทึกโดยใช้ SimpleFormatter
และ XMLFormatter
ใน Crunchify เราได้เผยแพร่บทช่วยสอนบางประการเกี่ยวกับการเข้าสู่ระบบในช่วงเวลาหนึ่ง
- กำหนดค่า log4j.properties ทางที่ถูกต้อง
- ยูทิลิตี้ Log4j ที่พร้อมใช้งานสำหรับโปรเจ็กต์การผลิตของคุณ
- สร้างระดับการบันทึกของคุณเอง
วันนี้ ฉันกำลังดำเนินการตั้งค่า Elastic FileBeats
บนโฮสต์ของฉัน และต้องการสร้างบันทึกที่ Elastic FileBeats สามารถรับได้ในขณะใช้งานจริง
ฉันได้สร้างยูทิลิตี้สร้างบันทึกอย่างรวดเร็วและต้องการแบ่งปันสิ่งเดียวกันนี้กับทุกคน เราจะใช้ยูทิลิตี้ java.util.logging
ในตัวของจาวา
CrunchifyLogGenerator.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
package crunchify . com . tutorial ; import java . io . IOException ; import java . util . logging . FileHandler ; import java . util . logging . Logger ; import java . util . logging . SimpleFormatter ; import java . util . logging . XMLFormatter ; /** * @author Crunchify.com * Version: 1.0 * Simplest way to generate logs files using SimpleFormatter and XMLFormatter */ public class CrunchifyLogGenerator { public static void main ( String [ ] args ) { // A Logger object is used to log messages for a specific system or application // component. Loggers are normally named, using a hierarchical dot-separated // namespace. Logger names can be arbitrary strings, but they should normally be // based on the package name or class name of the logged component, such as // java.net or javax.swing. In addition it is possible to create "anonymous" // Loggers that are not stored in the Logger namespace. Logger crunchifyLogger = Logger . getLogger ( "crunchifyLog" ) ; // Simple file logging Handler. FileHandler crunchifyFileHandler ; try { // We are setting handler to true = append data to file crunchifyFileHandler = new FileHandler ( "/Users/ashah/crunchify/crunchify-log.log" , true ) ; crunchifyLogger . addHandler ( crunchifyFileHandler ) ; // Print a brief summary of the LogRecord in a human readable format. SimpleFormatter formatter = new SimpleFormatter ( ) ; crunchifyFileHandler . setFormatter ( formatter ) ; // Format a LogRecord into a standard XML format. Uncomment below 2 lines to see XML result. // XMLFormatter formatter2 = new XMLFormatter(); // crunchifyFileHandler.setFormatter(formatter2); int n = 1 ; // infinite loop while ( true ) { // Log an INFO message. crunchifyLogger . info ( "Adding Crunchify Log line: " + n ) ; Thread . sleep ( 1000 ) ; n ++ ; } } catch ( SecurityException e ) { e . printStackTrace ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } catch ( InterruptedException e ) { e . printStackTrace ( ) ; } } } |
ดังที่คุณเห็นในโปรแกรมด้านบน ฉันได้แสดงความคิดเห็นไว้ below 2 lines
เมื่อคุณเรียกใช้โปรแกรมด้านบน ให้ยกเลิกการแสดงข้อคิดเห็นด้านล่างสองบรรทัด แล้วคุณจะเห็นผลลัพธ์ผลลัพธ์ XML
1 2 |
// XMLFormatter formatter2 = new XMLFormatter(); // crunchifyFileHandler.setFormatter(formatter2); |
เพียงเรียกใช้โปรแกรมด้านบนเป็นแอปพลิเคชัน Java แล้วคุณจะเห็นบรรทัดด้านล่างเพิ่มในไฟล์บันทึกที่คุณระบุ
เอาต์พุตคอนโซล Eclipse:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
Jan 27 , 2019 7 : 04 : 11 PM crunchify . com . tutorial . CrunchifyLogGenerator main INFO : Adding Crunchify Log line : 3 Jan 27 , 2019 7 : 04 : 12 PM crunchify . com . tutorial . CrunchifyLogGenerator main INFO : Adding Crunchify Log line : 4 Jan 27 , 2019 7 : 04 : 13 PM crunchify . com . tutorial . CrunchifyLogGenerator main INFO : Adding Crunchify Log line : 5 Jan 27 , 2019 7 : 04 : 14 PM crunchify . com . tutorial . CrunchifyLogGenerator main INFO : Adding Crunchify Log line : 6 <? xml version = "1.0" encoding = "UTF-8" standalone = "no" ?> < ! DOCTYPE log SYSTEM "logger.dtd" > < log > < record > < date > 2019 - 01 - 27T19 : 07 : 37 < / date > < millis > 1548637657211 < / millis > < sequence > 0 < / sequence > < logger > crunchifyLog < / logger > < level > INFO < / level > < class > crunchify . com . tutorial . CrunchifyLogGenerator < / class > < method > main < / method > < thread > 1 < / thread > < message > Adding Crunchify Log line : 1 < / message > < / record > < record > < date > 2019 - 01 - 27T19 : 07 : 38 < / date > < millis > 1548637658233 < / millis > < sequence > 1 < / sequence > < logger > crunchifyLog < / logger > < level > INFO < / level > < class > crunchify . com . tutorial . CrunchifyLogGenerator < / class > < method > main < / method > < thread > 1 < / thread > < message > Adding Crunchify Log line : 2 < / message > < / record > < record > < date > 2019 - 01 - 27T19 : 07 : 39 < / date > < millis > 1548637659236 < / millis > < sequence > 2 < / sequence > < logger > crunchifyLog < / logger > < level > INFO < / level > < class > crunchify . com . tutorial . CrunchifyLogGenerator < / class > < method > main < / method > < thread > 1 < / thread > < message > Adding Crunchify Log line : 3 < / message > < / record > < record > < date > 2019 - 01 - 27T19 : 07 : 40 < / date > < millis > 1548637660244 < / millis > < sequence > 3 < / sequence > < logger > crunchifyLog < / logger > < level > INFO < / level > < class > crunchify . com . tutorial . CrunchifyLogGenerator < / class > < method > main < / method > < thread > 1 < / thread > < message > Adding Crunchify Log line : 4 < / message > < / record > |
ฉันหวังว่าคุณจะพบว่ามีประโยชน์สำหรับโครงการของคุณ หากคุณประสบปัญหาใด ๆ ในการดำเนินการนี้โปรดแจ้งให้เราทราบ
