All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ericsson.research.trap.utils.JDKLoggerConfig Maven / Gradle / Ivy

There is a newer version: 1.4.2
Show newest version
package com.ericsson.research.trap.utils;

/*
 * ##_BEGIN_LICENSE_##
 * Transport Abstraction Package (trap)
 * ----------
 * Copyright (C) 2014 Ericsson AB
 * ----------
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * 3. Neither the name of the Ericsson AB nor the names of its contributors
 *    may be used to endorse or promote products derived from this software without
 *    specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * ##_END_LICENSE_##
 */

import java.util.Calendar;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.ConsoleHandler;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

public class JDKLoggerConfig
{
	
	static Set	savedLoggers	= Collections.synchronizedSet(new HashSet());
	
	public static void initForPrefixes(final Level level, String... prefixes)
	{
		ConsoleHandler h = new ConsoleHandler() {
			
			@Override
			public void publish(LogRecord record)
			{
				if (level.intValue() > record.getLevel().intValue())
					return;
				
				String formatted = this.getFormatter().format(record);
				System.out.print(formatted);
			}
			
		};
		h.setLevel(level);
		h.setFormatter(new Formatter() {
			
			@Override
			public String format(LogRecord record)
			{
				String ln = record.getLoggerName();
				int length = Math.max(ln.length() - 25, 0);
				String name = ln.substring(length);
				Calendar c = Calendar.getInstance();
				c.setTimeInMillis(record.getMillis());
				int hour = c.get(Calendar.HOUR_OF_DAY);
				int minute = c.get(Calendar.MINUTE);
				int second = c.get(Calendar.SECOND);
				int ms = c.get(Calendar.MILLISECOND);
				return "[" + String.format("%5.5s", record.getLevel()) + "] " + String.format("%02d:%02d:%02d.%03d", hour, minute, second, ms) + " {" + String.format("%25.25s", name) + "} - " + record.getMessage() + "\n";
			}
		});
		
		initForPrefixes(level, h, prefixes);
	}
	
	public static void initForPrefixes(final Level level, final Handler h, String... prefixes)
	{
		
		if (prefixes == null || prefixes.length == 0)
			processLogger("com.ericsson", level, h);
		else
			for (String prefix : prefixes)
				processLogger(prefix, level, h);
	}
	
	private static void processLogger(String string, Level level, Handler handler)
	{
		Logger logger = Logger.getLogger(string);
		logger.setLevel(level);
		for (Handler h : logger.getHandlers())
		    logger.removeHandler(h);
		logger.addHandler(handler);
		logger.setUseParentHandlers(false);
		savedLoggers.add(logger); // Prevent GC of the logger, thus storing our settings.
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy