Log classes on the Open Source TT framework

Source: Internet
Author: User
Tags tagname

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

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.