Static log operation

zhaozj2021-02-16  84

Import java.io. *; import java.util. *; / ** * Class specific usage: * * 1. Compile this class, newly established property profile: log.properties, and make sure you put it down you This * The location file content of the compiled class is as follows: Of course you can modify the path to the path you want * logfile = e: //logtext.log * 2. Example: * Use 1: * logwriter.log ("Zhang San landed the system"); * logwriter.log ("Zhang San deepened XXX record: record ID:"); * Using 2: * try {*} * catch (Exception ex) {* logwriter. LOG (EX); *} * * Some descriptions: * One. The getClass (). getResourceAsStream ("File Name") does not support Static calls, * So change the class to non-Static, but its call It is only called in outinit (), and Outinit () * is only called in the private constructor, and the private constructor can be called in static STATIC, * This will reach the use of static methods to call the log. Enter the log. And guarantee that there is only one example.

* II. If you understand the log4j, you can use the similar approach to encapsulate the log4j, to achieve a static convenient call. * *

Title: Static log operation class *

Description: *

Copyright: Copyright (c) 2004 *

Company: * @Author Hanic * @version 1.0 * / public class logwriter {private static final string defalutlogfilepathname = "C: / /logtext.log";// The default log file path and file name private static logwriter logwriter; // This class's unique instance private static inputStream Fin; // Property Private Properties PRO; // class Properties's supper is Hashtable class private static PrintWriter out; // output stream private static String logFileName; // output file name private LogWriter () {outInit (); // init out put stream, PrintWriter out instantiated objects.} / ** * Save the information you want to save in the log file, implement synchronization * Out Put the message infomation * @Param Message Infomation * / Public Stati c synchronized void log (string message) {if (Logwriter == Null || (out == null) {logwriter = new logwriter ();} if (out! = null) {Out.println (new java.util. Date () ":" message);}} / ** * Save the exception information in the log file, implement synchronization * Out Put the eXcection * @Param EX * / public static synchronized void log (Exception ex) { IF (logwriter == null || (out == null) logwriter = new logwriter (); if (out! = null) {Out.println (new java.util.date () ":"); EX. PrintStackTrace (OUT);

转载请注明原文地址:https://www.9cbs.com/read-13862.html

New Post(0)