Java Code Examples for org.apache.logging.log4j.Logger#log()

The following examples show how to use org.apache.logging.log4j.Logger#log() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LogFeederUtil.java    From ambari-logsearch with Apache License 2.0 6 votes vote down vote up
public static boolean logErrorMessageByInterval(String key, String message, Throwable e, Logger callerLogger, Level level) {
  LogHistory log = logHistoryList.get(key);
  if (log == null) {
    log = new LogHistory();
    logHistoryList.put(key, log);
  }

  if ((System.currentTimeMillis() - log.lastLogTime) > 30 * 1000) {
    log.lastLogTime = System.currentTimeMillis();
    if (log.counter > 0) {
      message += ". Messages suppressed before: " + log.counter;
    }
    log.counter = 0;
    callerLogger.log(level, message, e);

    return true;
  } else {
    log.counter++;
    return false;
  }
}
 
Example 2
Source File: Debug.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
public static void log(int level, Throwable t, String msg, String module, String callingClass, Object... params) {
    if (isOn(level) && isOnForThread(level)) { // SCIPIO: 2018-10-12: isOnForThread
        if (msg != null && params.length > 0) {
            StringBuilder sb = new StringBuilder();
            Formatter formatter = new Formatter(sb);
            formatter.format(msg, params);
            msg = sb.toString();
            formatter.close();
        }

        // log
        Logger logger = getLogger(module);
        logger.log(levelObjs[level], msg, t);
    }
}
 
Example 3
Source File: LogUtils.java    From CodeChickenLib with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static synchronized void errorOnce(Logger logger, Throwable t, String identifier, String format, Object... data) {
    StringWriter sw = new StringWriter();
    t.printStackTrace(new PrintWriter(sw));
    String stackTrace = identifier + sw.toString();
    synchronized (stackTraces) {
        if (!stackTraces.contains(stackTrace)) {
            logger.log(Level.ERROR, format, data, t);
            stackTraces.add(stackTrace);
        }
    }
}
 
Example 4
Source File: TestGenerator.java    From simpleretrogen with GNU General Public License v3.0 5 votes vote down vote up
@Mod.EventHandler
public void preinit(FMLPreInitializationEvent init)
{
    final Logger modLog = init.getModLog();
    IWorldGenerator gen = new IWorldGenerator() {
        @Override
        public void generate(Random random, int chunkX, int chunkZ, World world, IChunkGenerator chunkGenerator, IChunkProvider chunkProvider)
        {
            modLog.log(Level.INFO, "Calling!");
        }
    };
    GameRegistry.registerWorldGenerator(gen, 10);
}
 
Example 5
Source File: MelloriLogHandler.java    From vethrfolnir-mu with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void publish(LogRecord record)
{
	String loggerName = record.getLoggerName();
	if (loggerName == null)
	{
		loggerName = "";
	}
	
	Logger log = LogManager.getLogger(loggerName);
	
	org.apache.logging.log4j.Level level = levels.get(record.getLevel());
	
	if (level == null)
	{
		log.warn("Cannot find log4j level for: " + record.getLevel());
		level = org.apache.logging.log4j.Level.INFO;
	}
	
	String message = (record.getParameters() != null) && (record.getParameters().length > 0) ? MessageFormat.format(record.getMessage(), record.getParameters()) : record.getMessage();
	
	// Resource waster here
	// TODO: Finish all the logging then remove this
	String[] splits = record.getLoggerName().split("\\.");
	
	if(message.contains(splits[splits.length-1]+": "))
		message = message.replace(splits[splits.length-1]+":", "");
	
	log.log(level, message, record.getThrown());
}
 
Example 6
Source File: PrettyPrinter.java    From Mixin with MIT License 4 votes vote down vote up
private void logSpecial(Logger logger, Level level, ISpecialEntry line) {
    logger.log(level, "/*{}*/", line.toString());
}
 
Example 7
Source File: PrettyPrinter.java    From Mixin with MIT License 4 votes vote down vote up
private void logString(Logger logger, Level level, String line) {
    if (line != null) {
        logger.log(level, String.format("/* %-" + this.width + "s */", line));
    }
}
 
Example 8
Source File: LevelTest.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Test
public void testLevelLogging() {
    final Marker marker = MarkerManager.getMarker("marker");
    final Message msg = new ObjectMessage("msg");
    final Throwable t = new Throwable("test");
    final Level[] levels = new Level[] { Level.TRACE, Level.DEBUG, Level.INFO, Level.WARN, Level.ERROR, Level.FATAL };
    final String[] names = new String[] { "levelTest", "levelTest.Trace", "levelTest.Debug", "levelTest.Info",
            "levelTest.Warn", "levelTest.Error", "levelTest.Fatal" };
    for (final Level level : levels) {
        for (final String name : names) {
            final Logger logger = context.getLogger(name);
            logger.log(level, msg); // Message
            logger.log(level, 123); // Object
            logger.log(level, name); // String
            logger.log(level, marker, msg); // Marker, Message
            logger.log(level, marker, 123); // Marker, Object
            logger.log(level, marker, name); // marker, String
            logger.log(level, msg, t); // Message, Throwable
            logger.log(level, 123, t); // Object, Throwable
            logger.log(level, name, "param1", "param2"); // String, Object...
            logger.log(level, name, t); // String, Throwable
            logger.log(level, marker, msg, t); // Marker, Message, Throwable
            logger.log(level, marker, 123, t); // Marker, Object, Throwable
            logger.log(level, marker, name, "param1", "param2"); // Marker, String, Object...
            logger.log(level, marker, name, t); // Marker, String, Throwable
        }
    }
    // Logger "levelTest" will not receive same events as "levelTest.Trace"
    int levelCount = names.length - 1;

    final int UNIT = 14;
    final Expected[] expectedResults = new Expected[] { //
    new Expected(listAll, UNIT * levelCount, "TRACE", "All"), //
            new Expected(listTrace, UNIT * levelCount--, "TRACE", "Trace"), //
            new Expected(listDebug, UNIT * levelCount--, "DEBUG", "Debug"), //
            new Expected(listInfo, UNIT * levelCount--, "INFO", "Info"), //
            new Expected(listWarn, UNIT * levelCount--, "WARN", "Warn"), //
            new Expected(listError, UNIT * levelCount--, "ERROR", "Error"), //
            new Expected(listFatal, UNIT * levelCount--, "FATAL", "Fatal"), //
    };
    for (final Expected expected : expectedResults) {
        final String description = expected.description;
        final List<LogEvent> events = expected.appender.getEvents();
        assertNotNull(description + ": No events", events);
        assertThat(events, hasSize(expected.expectedEventCount));
        final LogEvent event = events.get(0);
        assertEquals(
            description + ": Expected level " + expected.expectedInitialEventLevel + ", got" + event.getLevel(),
            event.getLevel().name(), expected.expectedInitialEventLevel);
    }
}