
com.tectonica.log.LogConfigConsole Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tectonica-commons Show documentation
Show all versions of tectonica-commons Show documentation
Set of Java utility classes, all completely independent, to provide lightweight solutions for common situations
The newest version!
/*
* Copyright (C) 2014 Zach Melamed
*
* Latest version available online at https://github.com/zach-m/tectonica-commons
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.tectonica.log;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.LogManager;
/**
* Configures J.U.L to print to the console using a Tectonica formatter. To use:
*
* - add the following to your JVM arguments:
*
*
* -Djava.util.logging.config.class=com.tectonica.log.LogConfigConsole
*
*
* - optionally, if you want to state the minimum log level, add another argument, like this:
*
*
* -Dcom.tectonica.log.level=FINEST
*
*
* - optionally, if you want to provide your own formatter, add another argument, like this:
*
*
* -Dcom.tectonica.log.formatter=com.tectonica.log.LogFormat
*
*
*
*
* @author Zach Melamed
*/
public class LogConfigConsole
{
private static final String DEFAULT_LEVEL = Level.INFO.getName();
private static final String DEFAULT_FORMATTER = LogFormat.class.getName();
public LogConfigConsole() throws Exception
{
String level = System.getProperty("com.tectonica.log.level");
if (level == null || level.isEmpty())
level = DEFAULT_LEVEL;
String formatterClassName = System.getProperty("com.tectonica.log.formatter");
if (formatterClassName == null || formatterClassName.isEmpty())
formatterClassName = DEFAULT_FORMATTER;
StringBuilder props = new StringBuilder();
// configure levels
props.append(".level = ").append(level).append("\n");
// TODO: props.append("LOGGERNAME.level = ").append(SPECIFIC_LEVEL).append("\n");
// configure handlers
String con = ConsoleHandler.class.getName();
props.append("handlers = ").append(con).append("\n");
// configure console handler
props.append(con).append(".level = ALL").append("\n");
props.append(con).append(".formatter = ").append(formatterClassName).append("\n");
// apply configuration
try (InputStream ins = new ByteArrayInputStream(props.toString().getBytes()))
{
LogManager.getLogManager().readConfiguration(ins);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy