Tool class for printing records written to files on the imitation log4j Console

Source: Internet
Author: User

Background:

This afternoon, when I write a multi-threaded test program reader writer-writer first (this article will be published later, I want to see the printing result of the console, but you know, the eclipse console can only save part of the data. Log4j configuration is a little tricky.

So I wrote this tool class that can write local files myself:

 

Functions implemented by the tool class:

1. Follow[Time-call class-call method-row number of the call method-message]Press enter to write the file

2. One log file every day

If you don't talk about it, go to the Code:

 

Write2file. Java

Package COM. bankht. writerreader; import Java. io. file; import Java. io. filewriter; import Java. io. ioexception; import Java. util. calendar; import Java. util. date; import Java. util. gregoriancalendar;/*** @ Author: zhuyong * @ Creation Time: 03:10:30, December 7, ** @ Class description: write File */public class write2file {Private Static final string gettoday = new gregoriancalendar (). get (calendar. year) + "-" + new gregoriancalendar (). get (calendar. month) + "-" + new gregoriancalendar (). get (calendar. day_of_month) + "-"; Private Static final string filepath = "D:/" + gettoday + "log.txt"; Public void write2file (string message) {stacktraceelement stack [] = (New throwable ()). getstacktrace (); stacktraceelement S = stack [1]; string headermessage = S. getclassname () + ". "+ S. getmethodname () + "()" +"★Linenum: "+ S. getlinenumber () +"★Message: "; headermessage = adddatetimeheader (headermessage); message = headermessage + message +" \ r \ n "; filewriter = NULL; file = NULL; try {file = new file (filepath); filewriter = new filewriter (file, true); filewriter. write (Message);} catch (ioexception IE) {IE. printstacktrace ();} finally {try {filewriter. close ();} catch (ioexception IE) {IE. printstacktrace () ;}} Public String adddatetimeheader (string headermessage) {string datetimeheader = new date (). tolocalestring () +"★"; Return datetimeheader + = headermessage ;}}

Testwrite2file. Java

Package COM. bankht. writerreader; import Org. JUnit. test;/*** @ Author: zhuyong * @ Creation Time: 03:30:54 ** @ Class description: test File writing */public class testwrite2file {private write2file log = new write2file (); Private string message = NULL; @ testpublic void testwrite2file1 () {message = "ak- AK47 "; log. write2file (Message) ;}@ testpublic void testwrite2file2 () {message = "currently focused on cloud computing and agile development"; log. write2file (Message );}}

Display Effect:

 

File display:

17:26:02★Com. bankht. writerreader. testwrite2file. testwrite2file1 ()★Linenum: 18★Message: Special Forces-AK472012-6-7 17:26:05★Com. bankht. writerreader. testwrite2file. testwrite2file2 ()★Linenum: 23★Message: currently focuses on cloud computing and agile development 17:30:21★Com. bankht. writerreader. testwrite2file. testwrite2file2 ()★Linenum: 23★Message: currently focuses on cloud computing and agile development 17:30:39★Com. bankht. writerreader. testwrite2file. testwrite2file1 ()★Linenum: 18★Message: Special Forces-AK472012-6-7 17:31:50★Com. bankht. writerreader. testwrite2file. testwrite2file2 ()★Linenum: 23★Message: currently focuses on cloud computing and agile development 17:36:13★Com. bankht. writerreader. testwrite2file. testwrite2file1 ()★Linenum: 19★Message: Special Forces-AK472012-6-7 17:45:46★Com. bankht. writerreader. testwrite2file. testwrite2file2 ()★Linenum: 25★Message: currently focuses on cloud computing and agile development

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.