Import java.io. *; import java.util. *;
/ **? * Class specific use:? *? * 1. Compile this class, newly established property profile: log.properties, and make sure you put it down you? * Compile the location The contents of the file are 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 Dengluo The system ");? * Logwriter.log (" Zhang San deemed XXX record: record ID: ");? * Using 2:? * Try {? *}? * Catch (Exception EX) {? * ?? Logwriter.log (ex);? *}? *? * Some descriptions:? * 1. Where is GetClass (). GetResourceSstream ("file name") does not support Static calls, * So to change the class to Non-Static, but its call 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 is reached You can use a static method to invoke your logs at any time and ensure that there is only one instance. ? * 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
? * Description:? * Copyright: Copyright (c) 2004? * Company:? * @Author hanic? * @Version 1.0? * /
Public class logwriter {? private static final string ????? defalutlogfilepathname = "c: //logtext.log"; // The path and file name of the default log file? Private stat ?????? logwriter ?? logwriter ; ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? InputStream Fin; ????????????????????????????? // The input stream of the property configuration file? PRIVATE Static ?????? Properties? pro; ????????????????????????????????????????? // Class Properties's support is hashtable class? private static ?????? printwriter out; ?????????????????????????????? ??????????? // output stream? private static ?????? String ????? logfilename; ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ??????????? // Output file name
? private logwriter () {??? Outinit (); // init out put stream, instantiate the PrintWriter Out object.
? / ** ?? * Save the information you want to save in the log file, implement synchronization ?? * Out Put the message infomation ?? * @Param message infomation ?? * /? Public static 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 EXCETINFOMATION ?? * @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); ???}?}
? / ** ?? * Init ?? * /? Private void outinit () {??? f (logfilename == null) {????? logfilename {????? logfilename = getLogFileName (); // From attribute file Medium class get the path to log files ???} ??? Try {????? f (out == null) {// If the output I / O does not have an instance, generate a new ?????? ? out = new printwriter (new filewriter (true), true); ??????? // The meaning of the second parameter in the fileWriter () is: Is it added in the file??? ??} ???} ??? catch (ioException ex) {????? system.out.println ("Unable to open the log file:" logfilename); ????? EX.PrintStackTrace ();? ???? out = null; ???}?
? / ** ?? * According to the configuration file. Get the location of the log file ?? * ?? * @return logfilename ?? * /? Private string getLogfileName () {??? try {????? ing (pro) == NULL) {??????? pro = new java.util.properties (); ??????? fin = getClass (). getResourceAsStream ("log.properties"); // In class Current location, find the property profile log.properties ??????? Pro.Load (FIN); // Load the configuration file ??????? fin.close (); ?????}? ??} ??? catch (ioException ex) {????? system.err.println ("Unable to open the property configuration file: log.properties"); ????? EX.PrintStackTrace (); ??? } ??? Return Pro.getProperty ("logfile", defalutlogfilepathname); ??? // obtain the log file path according to the attribute value, the second parameter is: If the "logfile" flag is not found, the default value is returned? }? / ** ?? * You can also call this method when all logs are completed, release the resource. ?? * free all the resouce, this is secuty method ?? * /? Public void free () {??? try {????? this.logwriter = NULL; ????? f (out! = null) {??????? this.Out.close (); ????? } ????? if (fin! = null) {??????? this.fin.close (); ?????} ???} ??? catch (ioException ex) {?? ??? EX.PrintStackTrace (); ???}?}}