public class Logger {/*** log tag*/private String tagName = "Mogulogger";//tag name//private static int logLevel = LOG.V erbose;private static int logLevel = log.warn;private static Logger inst;private lock Lock;private Logger () {lock = new Re Entrantlock ();} public static synchronized Logger GetLogger (class<?> key) {if (inst = = null) {inst = new Logger ();} Return inst;} Private String Getfunctionname () {stacktraceelement[] sts = Thread.CurrentThread (). Getstacktrace (); if (sts = = null) { return null;} LOG.I ("TAG", "Start" + "ThreadName:" + Thread.class.getName () + "ClassName:" + this.getclass (). GetName ()); for ( Stacktraceelement st:sts) {log.i ("TAG", St.getclassname ());} LOG.I ("tag", "End"), for (Stacktraceelement st:sts) {log.i ("tag", St.getclassname ()), if (St.isnativemethod ()) { Continue;} if (St.getclassname (). Equals (Thread.class.getName ())) {continue;} if (St.getclassname (). Equals (This.getclass (). GetName ())) {continue;} Return "[" + st.getfilename () + ":" + st.getlinenumber () + "]";} REturn null;} private string CreateMessage (String msg) {string functionname = Getfunctionname (); Long threadId = Thread.CurrentThread () . GetId (); String currenttime = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss:SSS"). Format (new Date ()); String message = (functionname = = null? MSG: (functionname + "-" + string.valueof (threadId) + "-" + msg)); return Curr Enttime + "-" + message;} /*** log.i*/public void I (String format, Object ... args) {if (LogLevel <= log.info) {lock.lock (); try {String message = CreateMessage (getinputstring (format, args)); LOG.I (tagName, message);} finally {Lock.unlock ();}}} /*** log.v*/public void V (String format, Object ... args) {if (LogLevel <= log.verbose) {lock.lock (); try {String message = CreateMessage (getinputstring (format, args)); LOG.V (tagName, message);} finally {Lock.unlock ();}}} /*** log.d*/public void D (String format, Object ... args) {if (LogLevel <= log.debug) {lock.lock (); try {String message = CreateMessage (getinputstring (format, args)); LOG.D (TagName, Message);} finally {Lock.unlock ();}}} /*** log.e*/public void E (String format, Object ... args) {if (LogLevel <= log.error) {lock.lock (); try {String message = CreateMessage (getinputstring (format, args)); LOG.E (tagName, message);} finally {Lock.unlock ();}}} private string GetInputString (string format, Object ... args) {if (format = = null) {return "null log format";} return String.Format (format, args);} /*** log.error*/public void error (Exception e) {if (LogLevel <= log.error) {stringbuffer sb = new StringBuffer (); lock.l Ock (); try {String name = Getfunctionname (); stacktraceelement[] sts = E.getstacktrace (), if (name! = null) {sb.append (name + "-" + E + "\ r \ n");} else {sb.append (e + "\ r \ n");} if (sts = null && sts.length > 0) {for (stacktraceelement st:sts) {if (st! = null) {sb.append ("[" + ST.GETF Ilename () + ":" + st.getlinenumber () + "]\r\n");}}} LOG.E (TagName, sb.tostring ());} finally {Lock.unlock ();}}} /*** log.d*/public void W (String format, Object ... args) {if (LogLevel <= Log.warn) {lock.lock (); try {String message = CreateMessage (getinputstring (format, args)); LOG.W (tagName, message);} finally {Lock.unlock ();}}} /*** set log level*/public void SetLevel (int l) {lock.lock (); try {logLevel = l;} finally {Lock.unlock ();}}}
Log classes on the Open Source TT framework