Example usage for java.util.logging LogRecord getSourceMethodName

List of usage examples for java.util.logging LogRecord getSourceMethodName

Introduction

In this page you can find the example usage for java.util.logging LogRecord getSourceMethodName.

Prototype

public String getSourceMethodName() 

Source Link

Document

Get the name of the method that (allegedly) issued the logging request.

Usage

From source file:MainClass.java

public static void main(String[] args) {
    logger.setUseParentHandlers(false);/*  w  ww.  j  a va2  s  .c  om*/
    Handler conHdlr = new ConsoleHandler();
    conHdlr.setFormatter(new Formatter() {
        public String format(LogRecord record) {
            return record.getLevel() + "  :  " + record.getSourceClassName() + " -:- "
                    + record.getSourceMethodName() + " -:- " + record.getMessage() + "\n";
        }
    });
    logger.addHandler(conHdlr);
    logMessages();
}

From source file:Main.java

public static void main(String[] args) {
    logger.addHandler(new Handler() {
        public void publish(LogRecord logRecord) {
            strHolder.add(logRecord.getLevel() + ":");
            strHolder.add(logRecord.getSourceClassName() + ":");
            strHolder.add(logRecord.getSourceMethodName() + ":");
            strHolder.add("<" + logRecord.getMessage() + ">");
            strHolder.add("\n");
        }/*w  w  w. jav  a2s. c o m*/

        public void flush() {
        }

        public void close() {
        }
    });
    logger.warning("Logging Warning");
    logger.info("Logging Info");
    System.out.print(strHolder);
}

From source file:com.frostvoid.trekwar.server.TrekwarServer.java

/**
 * Initiates logging//from   w ww.  j a  v a 2  s  .  co  m
 *
 * @throws IOException
 */
private static void initLogging() throws IOException {
    FileHandler fh = new FileHandler(galaxyFileName + ".log");
    fh.setLevel(LOG.getLevel());
    Formatter logFormat = new Formatter() {

        @Override
        public String format(LogRecord rec) {
            StringBuilder buf = new StringBuilder(200);
            buf.append("#");
            buf.append(new java.util.Date());
            buf.append(' ');
            buf.append(rec.getLevel());
            buf.append(' ');
            buf.append(rec.getSourceClassName()).append(".").append(rec.getSourceMethodName());
            buf.append(":\n");
            buf.append(formatMessage(rec));
            buf.append('\n');
            return buf.toString();
        }
    };
    fh.setFormatter(logFormat);

    ConsoleHandler ch = new ConsoleHandler();
    ch.setLevel(LOG.getLevel());
    Formatter conlogFormat = new Formatter() {
        @Override
        public String format(LogRecord rec) {
            StringBuilder buf = new StringBuilder(200);
            buf.append(rec.getLevel());
            buf.append(": ");
            buf.append(formatMessage(rec));
            buf.append('\n');
            return buf.toString();
        }
    };
    ch.setFormatter(conlogFormat);

    LOG.addHandler(fh);
    LOG.addHandler(ch);
}

From source file:CustomFormatter.java

public synchronized String format(LogRecord record) {
    String methodName = record.getSourceMethodName();
    String message = record.getMessage();
    StringBuffer buffer = new StringBuffer(50);
    buffer.append(methodName);//from ww w  . j a  v a 2 s . c  o  m
    buffer.append("=");
    buffer.append(message);
    return buffer.toString();
}

From source file:io.stallion.services.LogFormatter.java

@Override
public String format(LogRecord record) {
    StringBuilder sb = new StringBuilder();
    SimpleDateFormat df = new SimpleDateFormat("MMM dd HH:mm:ss");

    String dateString = df.format(new Date(record.getMillis()));
    String classAndMethod = record.getSourceClassName() + "." + record.getSourceMethodName() + " ";
    classAndMethod = StringUtils.rightPad(classAndMethod, 50);
    sb.append(dateString).append(" ").append(record.getLevel().getLocalizedName()).append(" ")
            .append(classAndMethod).append(formatMessage(record)).append(LINE_SEPARATOR);

    if (record.getThrown() != null) {
        try {//  w  ww.  j  ava  2  s.  com
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            record.getThrown().printStackTrace(pw);
            pw.close();
            sb.append(sw.toString());
        } catch (Exception ex) {
            // ignore
        }
    }

    return sb.toString();
}

From source file:com.l2jfree.util.logging.L2RuntimeLogFormatter.java

@Override
protected final void format0(LogRecord record, TextBuilder tb) {
    tb.append(record.getLevel()).append(" ");

    appendDate(record, tb);/*w  w  w.j av a 2s.c  o m*/

    if (record.getLevel().intValue() > Level.INFO.intValue() || record.getThrown() != null)
        if (!StringUtils.isEmpty(record.getSourceClassName())
                && !StringUtils.isEmpty(record.getSourceMethodName()))
            tb.append(record.getSourceClassName()).append(".").append(record.getSourceMethodName())
                    .append("(): ");

    appendMessage(record, tb);
    appendThrown(record, tb);
}

From source file:com.acuityph.commons.logging.SimpleFormatter.java

/**
 * {@inheritDoc}//from  ww w  .j a v a2 s  .  co m
 *
 * @see java.util.logging.Formatter#format(java.util.logging.LogRecord)
 */
@Override
public final String format(final LogRecord logRecord) {
    final StringBuilder sb = new StringBuilder();
    sb.append(LEVELS_MAP.get(logRecord.getLevel()));
    final DateFormat sdf = DateFormat.getDateTimeInstance();
    sb.append(sdf.format(new Date(logRecord.getMillis()))).append(' ');
    sb.append(getShortName(logRecord.getSourceClassName()));
    sb.append('.').append(logRecord.getSourceMethodName()).append(": ");
    sb.append(logRecord.getMessage()).append("\n");
    return sb.toString();
}

From source file:com.cloudbees.jenkins.support.SupportLogFormatter.java

@Override
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = {
        "DE_MIGHT_IGNORE" }, justification = "The exception wasn't thrown on our stack frame")
public String format(LogRecord record) {
    StringBuilder builder = new StringBuilder();
    builder.append(fdf.format(new Date(record.getMillis())));
    builder.append(" [id=").append(record.getThreadID()).append("]");

    builder.append("\t").append(record.getLevel().getName()).append("\t");

    if (record.getSourceMethodName() != null) {
        String sourceClass;// www  . j  av  a 2 s.co  m
        if (record.getSourceClassName() == null) {
            sourceClass = record.getLoggerName();
        } else {
            sourceClass = record.getSourceClassName();
        }

        builder.append(abbreviateClassName(sourceClass, 32)).append("#").append(record.getSourceMethodName());
    } else {
        String sourceClass;
        if (record.getSourceClassName() == null) {
            sourceClass = record.getLoggerName();
        } else {
            sourceClass = record.getSourceClassName();
        }
        builder.append(abbreviateClassName(sourceClass, 40));
    }

    builder.append(": ").append(formatMessage(record));

    if (record.getThrown() != null) {
        try {
            StringWriter writer = new StringWriter();
            PrintWriter out = new PrintWriter(writer);
            record.getThrown().printStackTrace(out);
            out.close();
            builder.append(writer.toString());
        } catch (Exception e) {
            // ignore
        }
    }

    builder.append("\n");
    return builder.toString();
}

From source file:ch.jamiete.hilda.music.MusicLogFormat.java

@Override
public final String format(final LogRecord record) {
    String log = "";

    log += this.sdf.format(new Date(record.getMillis()));
    log += " [" + record.getLevel().getLocalizedName().toUpperCase() + ']';

    if (record.getSourceClassName() != null) {
        final String[] split = record.getSourceClassName().split("\\.");
        log += " [" + split[(split.length == 1) ? 0 : (split.length - 1)] + ']';
    }//from w w w  .ja  va  2 s .  co  m

    if (record.getSourceMethodName() != null) {
        log += " (" + record.getSourceMethodName() + ')';
    }

    log += ' ' + record.getMessage();

    if (record.getThrown() != null) {
        /*log += "\n" + record.getThrown().getMessage();
                
        for (StackTraceElement element : record.getThrown().getStackTrace()) {
        log += "\n" + element.toString();
        }*/
        log += '\n' + ExceptionUtils.getStackTrace(record.getThrown());
    }

    log += System.getProperty("line.separator");

    return log;
}

From source file:NemaLogFormatter.java

/** Formats the record.
 * /*from www. j  ava  2s  .  c  om*/
 * @param record The log record to format
 * @return The formated record
 */
@Override
public String format(LogRecord record) {
    String className = record.getSourceClassName();

    String threadName = Thread.currentThread().getName();
    if (threadName != null && threadName.length() > MAX_THREAD_NAME_LENGTH) {
        threadName = threadName.substring(threadName.length() - MAX_THREAD_NAME_LENGTH);
    }

    String sTimeStamp = FORMATER.format(new Date(record.getMillis()));

    return sTimeStamp + "::" + record.getLevel() + ":  " + record.getMessage() + "  "
            + ((bClass) ? " [" + className + "." + record.getSourceMethodName() + "]" : "")
            + ((bClass) ? " <" + threadName + ":" + record.getThreadID() + ">" : "") + NEW_LINE;
}