Example usage for java.util.logging Logger log

List of usage examples for java.util.logging Logger log

Introduction

In this page you can find the example usage for java.util.logging Logger log.

Prototype

public void log(LogRecord record) 

Source Link

Document

Log a LogRecord.

Usage

From source file:Main.java

public static void main(String args[]) throws Exception {
    Logger logger = Logger.getLogger("your.logging");
    ConsoleHandler handler = new ConsoleHandler();
    MemoryHandler mHandler = new MemoryHandler(handler, 10, Level.ALL);
    logger.addHandler(mHandler);//  ww  w. j av  a2  s.c o m
    logger.setUseParentHandlers(false);
    LogRecord record1 = new LogRecord(Level.SEVERE, "This is SEVERE level message");
    LogRecord record2 = new LogRecord(Level.WARNING, "This is WARNING level message");
    logger.log(record1);
    logger.log(record2);
}

From source file:com.basp.trabajo_al_minuto.model.business.BusinessUtils.java

public static void writeLog(String file, List<LogRecord> records) throws BusinessException {
    try {//w  ww.  ja va  2  s  . co m
        Logger logger = Logger.getAnonymousLogger();
        SimpleFormatter formatter = new SimpleFormatter();
        FileHandler fh = new FileHandler(file, Boolean.TRUE);
        fh.setFormatter(formatter);
        logger.addHandler(fh);
        logger.setUseParentHandlers(false);
        records.stream().forEach((logRecord) -> {
            logger.log(logRecord);
        });
    } catch (IOException | SecurityException ex) {
        Logger.getLogger(BusinessUtils.class.getName()).log(Level.SEVERE, "BusinessUtils.writeLog Exception",
                ex);
        throw new BusinessException(ex);
    }
}

From source file:fr.opensagres.xdocreport.core.logging.LogUtils.java

private static void doLog(Logger log, Level level, String msg, Throwable t) {
    LogRecord record = new LogRecord(level, msg);

    record.setLoggerName(log.getName());
    record.setResourceBundleName(log.getResourceBundleName());
    record.setResourceBundle(log.getResourceBundle());

    if (t != null) {
        record.setThrown(t);/*from   ww w . j a v a 2 s  . c  o  m*/
    }

    // try to get the right class name/method name - just trace
    // back the stack till we get out of this class
    StackTraceElement stack[] = (new Throwable()).getStackTrace();
    String cname = LogUtils.class.getName();
    for (int x = 0; x < stack.length; x++) {
        StackTraceElement frame = stack[x];
        if (!frame.getClassName().equals(cname)) {
            record.setSourceClassName(frame.getClassName());
            record.setSourceMethodName(frame.getMethodName());
            break;
        }
    }
    log.log(record);
}

From source file:org.apache.cxf.common.logging.LogUtils.java

private static void doLog(Logger log, Level level, String msg, Throwable t) {
    LogRecord record = new LogRecord(level, msg);

    record.setLoggerName(log.getName());
    record.setResourceBundleName(log.getResourceBundleName());
    record.setResourceBundle(log.getResourceBundle());

    if (t != null) {
        record.setThrown(t);/*w w w.  j  av a 2s . c  o  m*/
    }

    //try to get the right class name/method name - just trace
    //back the stack till we get out of this class
    StackTraceElement stack[] = (new Throwable()).getStackTrace();
    String cname = LogUtils.class.getName();
    for (int x = 0; x < stack.length; x++) {
        StackTraceElement frame = stack[x];
        if (!frame.getClassName().equals(cname)) {
            record.setSourceClassName(frame.getClassName());
            record.setSourceMethodName(frame.getMethodName());
            break;
        }
    }
    log.log(record);
}

From source file:com.machinepublishers.jbrowserdriver.JBrowserDriver.java

private static void log(Logger logger, String prefix, String message) {
    if (logger != null && !filteredLogs.contains(message)) {
        LogRecord record = null;//from   ww w  . ja  va2  s  .  c  o  m
        if (message.startsWith(">")) {
            String[] parts = message.substring(1).split("/", 3);
            record = new LogRecord(Level.parse(parts[0]),
                    new StringBuilder().append(prefix).append(" ").append(parts[2]).toString());
            record.setSourceMethodName(parts[1]);
            record.setSourceClassName(JBrowserDriver.class.getName());
        } else {
            record = new LogRecord(Level.WARNING,
                    new StringBuilder().append(prefix).append(" ").append(message).toString());
            record.setSourceMethodName(null);
            record.setSourceClassName(JBrowserDriver.class.getName());
        }
        logger.log(record);
    }
}