Search in sources :

Example 21 with Formatter

use of java.util.logging.Formatter in project zxing by zxing.

the class ServletContextLogHandler method publish.

@Override
public void publish(LogRecord record) {
    Formatter formatter = getFormatter();
    String message;
    if (formatter == null) {
        message = record.getMessage();
    } else {
        message = formatter.format(record);
    }
    Throwable throwable = record.getThrown();
    if (throwable == null) {
        context.log(message);
    } else {
        context.log(message, throwable);
    }
}
Also used : Formatter(java.util.logging.Formatter)

Example 22 with Formatter

use of java.util.logging.Formatter in project OpenAM by OpenRock.

the class DBHandler method configure.

private void configure() throws NullLocationException, FormatterInitException {
    setFilter(null);
    try {
        setEncoding("UTF-8");
    } catch (UnsupportedEncodingException e) {
        Debug.error(tableName + ":DBHandler: unsupportedEncodingException ", e);
    }
    String bufferSize = lmanager.getProperty(LogConstants.BUFFER_SIZE);
    if (bufferSize != null && bufferSize.length() > 0) {
        try {
            recCountLimit = Integer.parseInt(bufferSize);
        } catch (NumberFormatException nfe) {
            Debug.warning(tableName + ":DBHandler: NumberFormatException ", nfe);
            if (Debug.messageEnabled()) {
                Debug.message(tableName + ":DBHandler: Setting buffer size to 1");
            }
            recCountLimit = 1;
        }
    } else {
        Debug.warning(tableName + ":DBHandler: Invalid buffer size: " + bufferSize);
        if (Debug.messageEnabled()) {
            Debug.message(tableName + ":DBHandler: Setting buffer size to 1");
        }
        recCountLimit = 1;
    }
    String recMaxDBMemStr = lmanager.getProperty(LogConstants.DB_MEM_MAX_RECS);
    if (recMaxDBMemStr != null && recMaxDBMemStr.length() > 0) {
        try {
            recMaxDBMem = Integer.parseInt(recMaxDBMemStr);
        } catch (NumberFormatException nfe) {
            Debug.warning(tableName + ":DBHandler:recMaxDBMem (" + recMaxDBMemStr + "): NumberFormatException ", nfe);
            if (Debug.messageEnabled()) {
                Debug.message(tableName + ":DBHandler: Setting Max DB Mem Buffer Size to 2x (" + 2 * recCountLimit + ") the Buffer Size (" + recCountLimit + ")");
            }
            recMaxDBMem = 2 * recCountLimit;
        }
    } else {
        Debug.warning(tableName + ":DBHandler: Invalid buffer size: " + bufferSize);
        if (Debug.messageEnabled()) {
            Debug.message(tableName + ":DBHandler: Defaulting Max DB Mem Buffer Size to 2x Buffer Size");
        }
        recMaxDBMem = 2 * recCountLimit;
    }
    if (recMaxDBMem < recCountLimit) {
        Debug.warning(tableName + ":DBHandler:Maximum DB memory buffer size < Buffer Size, " + "setting to buffer size (" + recCountLimit + ")");
        recMaxDBMem = recCountLimit;
    }
    String status = lmanager.getProperty(LogConstants.TIME_BUFFERING_STATUS);
    if (status != null && status.equalsIgnoreCase("ON")) {
        timeBufferingEnabled = true;
    }
    oraDataType = lmanager.getProperty(LogConstants.ORA_DBDATA_FIELDTYPE);
    mysqlDataType = lmanager.getProperty(LogConstants.MYSQL_DBDATA_FIELDTYPE);
    databaseURL = lmanager.getProperty(LogConstants.LOG_LOCATION);
    if ((databaseURL == null) || (databaseURL.length() == 0)) {
        throw new NullLocationException("Database URL location is null");
    }
    String strFormatter = com.sun.identity.log.LogManager.FORMATTER;
    if ((strFormatter == null) || (strFormatter.length() == 0)) {
        throw new FormatterInitException("Unable To Initialize DBFormatter");
    }
    userName = lmanager.getProperty(LogConstants.DB_USER);
    if ((userName == null) || (userName.length() == 0)) {
        throw new NullLocationException("userName is null");
    }
    password = lmanager.getProperty(LogConstants.DB_PASSWORD);
    if ((password == null) || (password.length() == 0)) {
        throw new NullLocationException("password not provided");
    }
    driver = lmanager.getProperty(LogConstants.DB_DRIVER);
    if ((driver == null) || (driver.length() == 0)) {
        throw new NullLocationException("driver not provided");
    }
    //
    if (driver.toLowerCase().indexOf("oracle") != -1) {
        isMySQL = false;
    } else if (driver.toLowerCase().indexOf("mysql") != -1) {
        isMySQL = true;
    } else {
        isMySQL = false;
        Debug.warning(tableName + ":DBHandler:configure:assuming driver: '" + driver + "' is Oracle-compatible.");
    }
    try {
        Class clz = Class.forName(strFormatter);
        Formatter formatter = (Formatter) clz.newInstance();
        setFormatter(formatter);
    } catch (Exception e) {
        // should be Invalid Formatter Exception
        Debug.error(tableName + ":DBHandler: Could not load Formatter", e);
        throw new FormatterInitException("Unable To Initialize DBFormatter " + e.getMessage());
    }
}
Also used : Formatter(java.util.logging.Formatter) NullLocationException(com.iplanet.log.NullLocationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NullLocationException(com.iplanet.log.NullLocationException) ConnectionException(com.iplanet.log.ConnectionException) DriverLoadException(com.iplanet.log.DriverLoadException) SQLException(java.sql.SQLException) AMLogException(com.sun.identity.log.AMLogException) ThreadPoolException(com.iplanet.am.util.ThreadPoolException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 23 with Formatter

use of java.util.logging.Formatter in project ACS by ACS-Community.

the class CommandCenter method main.

public static void main(String[] args) {
    for (Handler h : Logger.getLogger("").getHandlers()) {
        if (h instanceof ConsoleHandler) {
            h.setFormatter(new Formatter() {

                DateFormat df = new SimpleDateFormat("HH:mm:ss ");

                @Override
                public String format(LogRecord record) {
                    String s = df.format(new Date(record.getMillis()));
                    s += record.getMessage() + "\n";
                    return s;
                }
            });
        }
    }
    // --- parse the command line
    StartupOptions startupOptions = new StartupOptions();
    for (int i = 0; i < args.length; i++) {
        if (i == 0 && equalsOneOf(args[i], new String[] { "-h", "-help", "--help" })) {
            printUsage(System.out);
            return;
        }
        try {
            if (equalsOneOf(args[i], new String[] { "-r", "-retrieve", "--retrieve" })) {
                startupOptions.project = new File(args[++i]);
                continue;
            }
            if (equalsOneOf(args[i], new String[] { "-g", "-geometry", "--geometry" })) {
                StringTokenizer toky = new StringTokenizer(args[++i], "x+");
                int w = Integer.parseInt(toky.nextToken());
                int h = Integer.parseInt(toky.nextToken());
                int x = Integer.parseInt(toky.nextToken());
                int y = Integer.parseInt(toky.nextToken());
                startupOptions.geometry = new Rectangle(x, y, w, h);
                continue;
            }
            if (equalsOneOf(args[i], new String[] { "-x", "-noexit", "--noexit" })) {
                startupOptions.doExitOnClose = false;
                continue;
            }
            if (equalsOneOf(args[i], new String[] { "-useNativeSSH", "--useNativeSSH" })) {
                log.warning("command line option '-useNativeSSH' no longer supported.");
                continue;
            }
            if (equalsOneOf(args[i], new String[] { "-killNativeSSH", "--killNativeSSH" })) {
                log.warning("command line option '-killNativeSSH' no longer supported.");
                continue;
            }
            // msc (Oct 24, 2005): stand-alone argument should be considered a project name
            // msc (Apr 28, 2006): alternatively it could be a manager location
            String standalone = args[i];
            if (standalone.length() > 8 && standalone.substring(0, 8).equalsIgnoreCase("corbaloc")) {
                startupOptions.manager = standalone;
                continue;
            }
            String mgrArg = MiscUtils.convertShortNotationToCorbaloc(standalone);
            if (mgrArg != null) {
                startupOptions.manager = mgrArg;
                continue;
            }
            startupOptions.project = new File(standalone);
        } catch (Exception exc) {
            // ArrayIndexOutOfBounds, NumberFormat, ...
            startupOptions = new StartupOptions();
            log.warning("command line argument(s) invalid, some arguments may be ignored: " + Arrays.asList(args));
            printUsage(System.err);
        }
    }
    // --- instantiate appropriate logic
    commandCenterLogic = new CommandCenterLogic();
    commandCenterLogic.prepare(startupOptions);
    if (startupOptions.project != null) {
        commandCenterLogic.loadProject(startupOptions.project);
    }
    commandCenterLogic.go();
}
Also used : CommandCenterLogic(alma.acs.commandcenter.app.CommandCenterLogic) Formatter(java.util.logging.Formatter) Rectangle(java.awt.Rectangle) ConsoleHandler(java.util.logging.ConsoleHandler) Handler(java.util.logging.Handler) ConsoleHandler(java.util.logging.ConsoleHandler) Date(java.util.Date) StringTokenizer(java.util.StringTokenizer) LogRecord(java.util.logging.LogRecord) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File) StartupOptions(alma.acs.commandcenter.app.CommandCenterLogic.StartupOptions)

Example 24 with Formatter

use of java.util.logging.Formatter in project google-cloud-java by GoogleCloudPlatform.

the class LoggingHandlerTest method testReportFormatError.

@Test
public void testReportFormatError() {
    expect(options.getProjectId()).andReturn(PROJECT).anyTimes();
    expect(options.getService()).andReturn(logging);
    logging.setFlushSeverity(Severity.ERROR);
    expectLastCall().once();
    logging.setWriteSynchronicity(Synchronicity.ASYNC);
    expectLastCall().once();
    replay(options, logging);
    Formatter formatter = EasyMock.createStrictMock(Formatter.class);
    RuntimeException ex = new RuntimeException();
    ErrorManager errorManager = EasyMock.createStrictMock(ErrorManager.class);
    errorManager.error(null, ex, ErrorManager.FORMAT_FAILURE);
    expectLastCall().once();
    LogRecord record = newLogRecord(Level.FINEST, MESSAGE);
    expect(formatter.format(record)).andThrow(ex);
    replay(errorManager, formatter);
    Handler handler = new LoggingHandler(LOG_NAME, options);
    handler.setLevel(Level.ALL);
    handler.setErrorManager(errorManager);
    handler.setFormatter(formatter);
    handler.publish(record);
    verify(errorManager, formatter);
}
Also used : ErrorManager(java.util.logging.ErrorManager) LogRecord(java.util.logging.LogRecord) Formatter(java.util.logging.Formatter) Handler(java.util.logging.Handler) Test(org.junit.Test)

Example 25 with Formatter

use of java.util.logging.Formatter in project qpid-broker-j by apache.

the class Slf4jLoggingHandler method publish.

@Override
public void publish(final LogRecord record) {
    MappedLevel level = convertLevel(record);
    final Logger logger = getLogger(record.getLoggerName());
    if (level.isEnabled(logger)) {
        Formatter formatter = getFormatter();
        try {
            String message = formatter.format(record);
            try {
                level.log(logger, message);
            } catch (RuntimeException e) {
                reportError(null, e, ErrorManager.WRITE_FAILURE);
            }
        } catch (RuntimeException e) {
            reportError(null, e, ErrorManager.FORMAT_FAILURE);
        }
    }
}
Also used : Formatter(java.util.logging.Formatter) Logger(org.slf4j.Logger)

Aggregations

Formatter (java.util.logging.Formatter)44 LogRecord (java.util.logging.LogRecord)18 File (java.io.File)13 SimpleFormatter (java.util.logging.SimpleFormatter)12 IOException (java.io.IOException)11 Logger (java.util.logging.Logger)11 FileHandler (java.util.logging.FileHandler)10 Handler (java.util.logging.Handler)9 Date (java.util.Date)6 Test (org.junit.Test)6 SimpleDateFormat (java.text.SimpleDateFormat)5 ConsoleHandler (java.util.logging.ConsoleHandler)5 Config (edu.neu.ccs.pyramid.configuration.Config)4 JSONLogFormatter (fish.payara.enterprise.server.logging.JSONLogFormatter)4 Level (java.util.logging.Level)4 StreamHandler (java.util.logging.StreamHandler)4 Pair (edu.neu.ccs.pyramid.util.Pair)3 DateFormat (java.text.DateFormat)3 ErrorManager (java.util.logging.ErrorManager)3 Filter (java.util.logging.Filter)3