Example usage for java.util.logging LogRecord getSourceClassName

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

Introduction

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

Prototype

public String getSourceClassName() 

Source Link

Document

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

Usage

From source file:MainClass.java

public static void main(String[] args) {
    logger.setUseParentHandlers(false);//from www . ja  v  a 2s.co m
    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 . j  a v  a  2 s  .c o  m*/

        public void flush() {
        }

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

From source file:org.freaknet.gtrends.client.GoogleTrendsClientFactory.java

private static void setLogLevel(CmdLineParser cmdLine) throws SecurityException, IllegalArgumentException {
    final Level level;
    if (cmdLine.getLogLevel() != null) {
        level = Level.parse(cmdLine.getLogLevel());
    } else {/* w w  w  .  j av a2 s  .  c om*/
        level = Level.parse(DEFAULT_LOGGING_LEVEL);
    }
    Logger log = LogManager.getLogManager().getLogger("");

    for (Handler h : log.getHandlers()) {
        log.removeHandler(h);
    }
    Handler handler = new ConsoleHandler();
    handler.setFormatter(new LogFormatter());
    handler.setLevel(level);
    log.setUseParentHandlers(false);

    Logger defaultLog = Logger.getLogger(GoogleConfigurator.getLoggerPrefix());
    defaultLog.addHandler(handler);
    defaultLog.setLevel(level);
    defaultLog.setFilter(new Filter() {
        @Override
        public boolean isLoggable(LogRecord record) {
            if (record.getSourceClassName().startsWith(GoogleConfigurator.getLoggerPrefix())) {
                return (record.getLevel().intValue() >= level.intValue());
            }
            return false;
        }
    });
}

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

/**
 * Initiates logging/*from  w  w w.ja 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:io.stallion.services.LogFilter.java

private String toPackageName(LogRecord record) {
    int i = record.getSourceClassName().lastIndexOf(".");
    if (i == -1) {
        return "";
    }/*from  www . j  a  v a 2s .c om*/
    return record.getSourceClassName().substring(0, i);
}

From source file:NemaLogFormatter.java

/** Formats the record.
 * //from   w  w w. j a  v a  2 s. co  m
 * @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;
}

From source file:ch.jamiete.hilda.LogFormat.java

@Override
public 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 ww .  j a v  a2s .co  m*/

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

    if (record.getThrown() != null) {
        log += System.getProperty("line.separator") + ExceptionUtils.getStackTrace(record.getThrown());
    }

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

    return log;
}

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 . j a v  a2s .  c om*/

    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: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 {/*from   w w  w.j  a  v  a2s .c o  m*/
            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);/*from   w w w. j a v  a 2  s  .  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);
}