Search in sources :

Example 6 with AcsLogLevelDefinition

use of alma.acs.logging.level.AcsLogLevelDefinition in project ACS by ACS-Community.

the class LogConfigTest method testDynamicChanges.

/**
	 * Tests runtime changes as they could happen via the {@linkplain LoggingConfigurable} API. 
	 */
public void testDynamicChanges() throws Exception {
    logger.info("============ Running testDynamicChanges ============");
    // the default log levels from schema defaults and optional env var setting
    AcsLogLevelDefinition defaultMinLogLevel = logConfig.getDefaultMinLogLevel();
    AcsLogLevelDefinition defaultMinLogLevelLocal = logConfig.getDefaultMinLogLevelLocal();
    // change the default log levels
    AcsLogLevelDefinition newDefaultLevel = defaultMinLogLevel.getNextHigherLevel();
    assertNotNull(newDefaultLevel);
    logConfig.setDefaultMinLogLevel(newDefaultLevel);
    assertSame(newDefaultLevel, logConfig.getDefaultMinLogLevel());
    assertSame(defaultMinLogLevelLocal, logConfig.getDefaultMinLogLevelLocal());
    newDefaultLevel = newDefaultLevel.getNextHigherLevel();
    assertNotNull(newDefaultLevel);
    logConfig.setDefaultMinLogLevel(newDefaultLevel);
    logConfig.setDefaultMinLogLevelLocal(newDefaultLevel);
    assertSame(newDefaultLevel, logConfig.getDefaultMinLogLevel());
    assertSame(newDefaultLevel, logConfig.getDefaultMinLogLevelLocal());
    // restore initial values
    logConfig.setDefaultMinLogLevel(defaultMinLogLevel);
    logConfig.setDefaultMinLogLevelLocal(defaultMinLogLevelLocal);
    // named logger levels 
    String knownLoggerName = "knownLogger";
    // now the logger is known, even though it has default values 
    UnnamedLogger knownLoggerConfig = logConfig.getNamedLoggerConfig(knownLoggerName);
    assertEquals(defaultMinLogLevel, AcsLogLevelDefinition.fromXsdLogLevel(knownLoggerConfig.getMinLogLevel()));
    assertEquals(defaultMinLogLevelLocal, AcsLogLevelDefinition.fromXsdLogLevel(knownLoggerConfig.getMinLogLevelLocal()));
    Set<String> loggerNames = logConfig.getLoggerNames();
    assertEquals(1, loggerNames.size());
    assertTrue(loggerNames.contains(knownLoggerName));
    String unknownLoggerName = "unknownLogger";
    // first encounter with this logger when setting its levels
    logConfig.setMinLogLevel(AcsLogLevelDefinition.DEBUG, unknownLoggerName);
    loggerNames = logConfig.getLoggerNames();
    assertEquals(2, loggerNames.size());
    assertTrue(loggerNames.contains(knownLoggerName));
    assertTrue(loggerNames.contains(unknownLoggerName));
    // make sure that returned objects are not "live" for the logConfig data
    knownLoggerConfig = logConfig.getNamedLoggerConfig(knownLoggerName);
    UnnamedLogger knownLoggerConfig2 = logConfig.getNamedLoggerConfig(knownLoggerName);
    assertNotSame(knownLoggerConfig, knownLoggerConfig2);
    newDefaultLevel = newDefaultLevel.getNextHigherLevel();
    assertNotNull(newDefaultLevel);
    knownLoggerConfig.setMinLogLevel(newDefaultLevel.toXsdLevel());
    assertSame(defaultMinLogLevel, logConfig.getDefaultMinLogLevel());
    newDefaultLevel = newDefaultLevel.getNextHigherLevel();
    assertNotNull(newDefaultLevel);
    logConfig.setDefaultMinLogLevel(newDefaultLevel);
    assertEquals(defaultMinLogLevel, AcsLogLevelDefinition.fromXsdLogLevel(knownLoggerConfig2.getMinLogLevel()));
}
Also used : UnnamedLogger(alma.maci.loggingconfig.UnnamedLogger) AcsLogLevelDefinition(alma.acs.logging.level.AcsLogLevelDefinition)

Example 7 with AcsLogLevelDefinition

use of alma.acs.logging.level.AcsLogLevelDefinition in project ACS by ACS-Community.

the class LogTypeCellRenderer method getTableCellRendererComponent.

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    if (isSelected) {
        levelCB.setBackground(table.getSelectionBackground());
        levelCB.setForeground(table.getSelectionForeground());
    } else {
        levelCB.setBackground(table.getBackground());
        levelCB.setForeground(table.getForeground());
    }
    // Set the text
    //minimum.setText(value.toString());
    levelCB.setFont(table.getFont());
    // If the type of log is known, set the icon
    LogTypeHelper logType;
    try {
        AcsLogLevelDefinition levelDef = AcsLogLevelDefinition.fromInteger(Integer.parseInt(value.toString()));
        logType = LogTypeHelper.fromAcsCoreLevel(levelDef);
    } catch (Exception e) {
        System.err.println("Error parsing a log type: " + value.toString());
        e.printStackTrace(System.err);
        logType = null;
    }
    if (logType != null) {
        levelCB.setSelectedIndex(logType.ordinal());
    }
    return levelCB;
}
Also used : LogTypeHelper(com.cosylab.logging.engine.log.LogTypeHelper) AcsLogLevelDefinition(alma.acs.logging.level.AcsLogLevelDefinition)

Example 8 with AcsLogLevelDefinition

use of alma.acs.logging.level.AcsLogLevelDefinition in project ACS by ACS-Community.

the class AcsBinLogFormatter method formatBinary.

LogBinaryRecord formatBinary(LogRecord logRecord) {
    LogBinaryRecord rLog = new LogBinaryRecord();
    // log level 
    AcsLogLevel acsLevel = AcsLogLevel.getNativeLevel(logRecord.getLevel());
    if (acsLevel == null) {
        return null;
    }
    final AcsLogLevelDefinition acsCoreLevel = acsLevel.getAcsLevel();
    // get date
    Date date = new Date(logRecord.getMillis());
    String TimeStamp = IsoDateFormat.formatDate(date);
    LogParameterUtil logParamUtil = new LogParameterUtil(logRecord);
    rLog.type = (short) acsCoreLevel.value;
    rLog.TimeStamp = TimeStamp;
    String file = logRecord.getSourceClassName();
    if (file == null) {
        if (acsCoreLevel == AcsLogLevelDefinition.DEBUG)
            rLog.File = "unknown";
    } else
        rLog.File = file;
    long line = logParamUtil.extractLongProperty(LogParameterUtil.PARAM_LINE, -1);
    if (line < 0) {
        if (acsCoreLevel == AcsLogLevelDefinition.TRACE || acsCoreLevel == AcsLogLevelDefinition.DEBUG)
            rLog.Line = 0;
    } else
        rLog.Line = (int) line;
    String Routine = logRecord.getSourceMethodName();
    if (Routine == null) {
        if (acsCoreLevel == AcsLogLevelDefinition.TRACE) {
            rLog.Routine = "unknown";
        }
    } else {
        rLog.Routine = Routine;
    }
    // host name: may be different from local host if ErrorTrace gets logged
    String hostName = logParamUtil.extractStringProperty(LogParameterUtil.PARAM_HOSTNAME, null);
    if (hostName == null || hostName.length() == 0) {
        hostName = getLocalHostName();
    }
    rLog.Host = hostName;
    //		String process = logParamUtil.extractStringProperty(LogParameterUtil.PARAM_PROCESSNAME, null);
    //		if (process != null)
    //            rLog.Process = process;
    //        else{
    String process = logRecord.getLoggerName();
    if (process != null)
        rLog.Process = "LoggerName: " + process;
    else
        rLog.Process = "";
    //      }
    // source object: the container name or component name
    String sourceObject = logRecord.getLoggerName();
    if (sourceObject != null) {
        rLog.SourceObject = sourceObject;
    } else
        rLog.SourceObject = "";
    // add thread ID, or name if given		
    String threadName = logParamUtil.extractStringProperty(LogParameterUtil.PARAM_THREAD_NAME, null);
    if (threadName != null && threadName.length() > 0)
        rLog.Thread = threadName;
    else if (logRecord.getThreadID() >= 0)
        rLog.Thread = "" + logRecord.getThreadID();
    else
        rLog.Thread = "";
    // add context		
    String context = logParamUtil.extractStringProperty("Context", null);
    if (context != null)
        rLog.LogContext = context;
    else
        rLog.LogContext = "";
    // add stack info
    rLog.StackId = "";
    rLog.StackLevel = 0;
    if (acsCoreLevel.compareTo(AcsLogLevelDefinition.WARNING) >= 0) {
        // add stack id
        String stackId = logParamUtil.extractStringProperty(LogParameterUtil.PARAM_STACK_ID, null);
        if (stackId == null)
            rLog.StackId = "unknown";
        else
            rLog.StackId = stackId;
        // add stack idlevel
        long stackLevel = logParamUtil.extractLongProperty(LogParameterUtil.PARAM_STACK_LEVEL, -1);
        if (stackLevel > 0)
            rLog.StackLevel = (int) stackLevel;
    }
    // add log id		
    long logId = logRecord.getSequenceNumber();
    if (logId >= 0)
        rLog.LogId = "" + logId;
    else
        rLog.LogId = "";
    // add URI		
    String uri = logParamUtil.extractStringProperty(LogParameterUtil.PARAM_URI, null);
    if (uri != null)
        rLog.Uri = uri;
    else
        rLog.Uri = "";
    // add priority
    // to be written only different as entry priority		
    long priority = logParamUtil.extractLongProperty(LogParameterUtil.PARAM_PRIORITY, acsCoreLevel.value);
    if (priority != acsCoreLevel.value)
        rLog.Priority = (int) priority;
    else
        rLog.Priority = -1;
    // the log message becomes the text in our XML record
    if (logRecord.getMessage() != null) {
        rLog.MsgData = logRecord.getMessage();
    } else
        rLog.MsgData = "";
    //add Audience, Array and Antenna, if applicable
    if (logRecord instanceof AcsLogRecord) {
        rLog.Audience = ((AcsLogRecord) logRecord).getAudience();
        rLog.Array = ((AcsLogRecord) logRecord).getArray();
        rLog.Antenna = ((AcsLogRecord) logRecord).getAntenna();
    }
    // <Data> elements: logged exception or error trace, and log parameters
    //NameValue a;
    //rLog.log_data = new NameValue[propertiesMap.size() + 1];
    //TODO: see why there are not attributes here
    rLog.attributes = new NameValue[0];
    NameValue[] log_data = new NameValue[257];
    //rLog.log_data = new NameValue[257];
    int i = 0;
    try {
        // logged exception
        Throwable loggedThrowable = logRecord.getThrown();
        if (loggedThrowable != null) {
            StringWriter exWriter = new StringWriter();
            loggedThrowable.printStackTrace(new PrintWriter(exWriter));
            log_data[i++] = new NameValue("LoggedException", exWriter.toString());
        }
        // log parameters (except for the special properties which were used already to set specific fields)
        for (Object param : logParamUtil.getNonSpecialPropertiesMapParameters()) {
            if (i >= 255)
                break;
            if (param instanceof Map) {
                // any map that is not the special properties map we interpret as name-value pairs.
                Map propertiesMap = (Map) param;
                for (Object keyName : propertiesMap.keySet()) {
                    String value = propertiesMap.get(keyName).toString();
                    log_data[i++] = new NameValue(keyName.toString(), value);
                }
            } else {
                // a single parameter was logged, but we have to fit it into our name-value scheme using a fake name
                String value = param.toString();
                log_data[i++] = new NameValue("LoggedParameter", value);
            }
        }
    } catch (Exception e) {
        // expected not to happen often at all, thus no try blocks inside every loop, so we may lose some <Data>
        String value = e.toString();
        log_data[i++] = new NameValue("DataConstructionError", value);
    }
    rLog.log_data = new NameValue[i];
    for (int j = 0; j < i; j++) {
        rLog.log_data[j] = log_data[j];
    }
    return rLog;
}
Also used : LogParameterUtil(alma.acs.logging.LogParameterUtil) LogBinaryRecord(alma.ACSLoggingLog.LogBinaryRecord) Date(java.util.Date) AcsLogRecord(alma.acs.logging.AcsLogRecord) NameValue(alma.ACSLoggingLog.NameValue) StringWriter(java.io.StringWriter) AcsLogLevel(alma.acs.logging.AcsLogLevel) AcsLogLevelDefinition(alma.acs.logging.level.AcsLogLevelDefinition) Map(java.util.Map) PrintWriter(java.io.PrintWriter)

Example 9 with AcsLogLevelDefinition

use of alma.acs.logging.level.AcsLogLevelDefinition in project ACS by ACS-Community.

the class AcsXMLLogFormatter method format.

/**
	 * Constructs the XML log message that can be sent to the ACS logging service.
	 * @see java.util.logging.Formatter#format(java.util.logging.LogRecord)
	 */
public String format(LogRecord logRecord) {
    // log level 
    AcsLogLevel acsLevel = AcsLogLevel.getNativeLevel(logRecord.getLevel());
    if (acsLevel == null) {
        return "";
    }
    final AcsLogLevelDefinition acsCoreLevel = acsLevel.getAcsLevel();
    final String levelName = acsLevel.getEntryName();
    // get date
    String TimeStamp = IsoDateFormat.formatDate(new Date(logRecord.getMillis()));
    LogParameterUtil logParamUtil = new LogParameterUtil(logRecord);
    StringBuffer sb = new StringBuffer("");
    sb.append("<");
    sb.append(levelName);
    sb.append(" ");
    sb.append("TimeStamp=\"" + TimeStamp + "\" ");
    String file = logRecord.getSourceClassName();
    if (file == null) {
        if (acsCoreLevel == AcsLogLevelDefinition.DEBUG)
            sb.append("File=\"unknown\" ");
    } else {
        sb.append("File=\"" + file + "\"  ");
    }
    long line = logParamUtil.extractLongProperty(LogParameterUtil.PARAM_LINE, -1);
    if (line < 0) {
        if (acsCoreLevel == AcsLogLevelDefinition.TRACE || acsCoreLevel == AcsLogLevelDefinition.DEBUG)
            sb.append("Line=\"0\" ");
    } else {
        sb.append("Line=\"" + line + "\" ");
    }
    String Routine = logRecord.getSourceMethodName();
    if (Routine == null) {
        if (acsCoreLevel == AcsLogLevelDefinition.TRACE)
            sb.append("Routine=\"unknown\" ");
    } else {
        sb.append("Routine=\"" + maskAttribute(Routine) + "\" ");
    }
    // host name: may be different from local host if ErrorTrace gets logged
    String hostName = logParamUtil.extractStringProperty(LogParameterUtil.PARAM_HOSTNAME, null);
    if (hostName == null || hostName.length() == 0) {
        hostName = this.getLocalHostName();
    }
    //hostName = getLocalHostName();
    sb.append("Host=\"" + hostName + "\" ");
    String process = logParamUtil.extractStringProperty(LogParameterUtil.PARAM_PROCESSNAME, null);
    if (process != null) {
        sb.append("Process=\"" + process + "\" ");
    } else {
        process = logRecord.getLoggerName();
        if (process != null) {
            sb.append("Process=\"" + process + "\" ");
        }
    }
    String sourceObject = logParamUtil.extractStringProperty(LogParameterUtil.PARAM_SOURCEOBJECT, null);
    if (sourceObject != null) {
        sb.append("SourceObject=\"" + sourceObject + "\" ");
    } else {
        sourceObject = logRecord.getLoggerName();
        if (sourceObject != null) {
            sb.append("SourceObject=\"" + sourceObject + "\" ");
        }
    }
    // add thread ID, or name if given		
    String threadName = logParamUtil.extractStringProperty(LogParameterUtil.PARAM_THREAD_NAME, null);
    if (threadName != null && threadName.length() > 0) {
        sb.append("Thread=\"" + threadName + "\" ");
    } else if (logRecord.getThreadID() >= 0) {
        sb.append("Thread=\"" + logRecord.getThreadID() + "\" ");
    }
    // add context		
    String context = logParamUtil.extractStringProperty("Context", null);
    if (context != null) {
        sb.append("Context=\"" + context + "\" ");
    }
    // add stack info
    if (acsCoreLevel.compareTo(AcsLogLevelDefinition.WARNING) >= 0) {
        // add stack id
        String stackId = logParamUtil.extractStringProperty(LogParameterUtil.PARAM_STACK_ID, null);
        if (stackId == null)
            sb.append("StackId=\"unknown\" ");
        else
            sb.append("StackId=\"" + stackId + "\" ");
        // add stack idlevel
        long stackLevel = logParamUtil.extractLongProperty(LogParameterUtil.PARAM_STACK_LEVEL, -1);
        if (stackLevel < 0)
            sb.append("StackLevel=\"0\" ");
        else
            sb.append("StackLevel=\"" + stackLevel + "\" ");
    }
    // add log id		
    long logId = logRecord.getSequenceNumber();
    if (logId >= 0) {
        sb.append("LogId=\"" + logId + "\" ");
    }
    // add URI		
    String uri = logParamUtil.extractStringProperty(LogParameterUtil.PARAM_URI, null);
    if (uri != null) {
        sb.append("Uri=\"" + uri + "\" ");
    }
    // add priority
    // to be written only different as entry priority		
    long priority = logParamUtil.extractLongProperty(LogParameterUtil.PARAM_PRIORITY, acsCoreLevel.value);
    if (priority != acsCoreLevel.value) {
        sb.append("Priority=\"" + priority + "\" ");
    }
    //add Audience, if applicable(for typeSafeLogs/Operator logs)
    if (logRecord instanceof AcsLogRecord) {
        String audience = ((AcsLogRecord) logRecord).getAudience();
        if (!audience.equals(""))
            sb.append("Audience=\"" + audience + "\" ");
        //add Array
        String array = ((AcsLogRecord) logRecord).getArray();
        if (!array.equals(""))
            sb.append("Array=\"" + array + "\" ");
        //add Antenna
        String antenna = ((AcsLogRecord) logRecord).getAntenna();
        if (!antenna.equals(""))
            sb.append("Antenna=\"" + antenna + "\" ");
    }
    sb.setCharAt(sb.lastIndexOf("") - 1, '>');
    // the log message becomes the text in our XML record
    if (logRecord.getMessage() != null) {
        sb.append(maskMessage(logRecord.getMessage()));
    }
    try {
        // logged exception
        Throwable loggedThrowable = logRecord.getThrown();
        if (loggedThrowable != null) {
            StringWriter exWriter = new StringWriter();
            loggedThrowable.printStackTrace(new PrintWriter(exWriter));
            sb.append("<Data Name=\"LoggedException\">" + maskMessage(exWriter.toString()) + "</Data>");
        }
        // log parameters (except for the special properties which were used already to set specific fields)
        for (Object param : logParamUtil.getNonSpecialPropertiesMapParameters()) {
            if (param instanceof Map) {
                // any map that is not the special properties map we interpret as name-value pairs.
                Map propertiesMap = (Map) param;
                for (Object keyName : propertiesMap.keySet()) {
                    String value = maskEmptyDataContent(propertiesMap.get(keyName).toString());
                    sb.append("<Data Name=\"" + keyName.toString() + "\">" + maskMessage(value) + "</Data>");
                }
            } else {
                // a single parameter was logged, but we have to fit it into our name-value scheme using a fake name
                String value = maskEmptyDataContent(param.toString());
                sb.append("<Data Name=\"LoggedParameter\">" + maskMessage(value) + "</Data>");
            }
        }
    } catch (Exception e) {
        // expected not to happen often at all, thus no try blocks inside every loop, so we may lose some <Data>
        sb.append("<Data Name=\"DataConstructionError\">" + maskMessage(e.toString()) + "</Data>");
    }
    // end tag of XML record
    sb.append("</" + levelName + ">");
    String Log = sb.toString();
    //		System.out.println("Logging XML log entry " + Log);
    return Log;
}
Also used : LogParameterUtil(alma.acs.logging.LogParameterUtil) Date(java.util.Date) AcsLogRecord(alma.acs.logging.AcsLogRecord) StringWriter(java.io.StringWriter) AcsLogLevel(alma.acs.logging.AcsLogLevel) AcsLogLevelDefinition(alma.acs.logging.level.AcsLogLevelDefinition) Map(java.util.Map) PrintWriter(java.io.PrintWriter)

Example 10 with AcsLogLevelDefinition

use of alma.acs.logging.level.AcsLogLevelDefinition in project ACS by ACS-Community.

the class StdOutConsoleHandler method configureLogging.

/**
	 * @see alma.acs.logging.config.LogConfigSubscriber#configureLogging(alma.acs.logging.config.LogConfig)
	 */
public void configureLogging(LogConfig newLogConfig) {
    // just in case some day LogConfig is no longer used as a singleton
    this.logConfig = newLogConfig;
    try {
        AcsLogLevelDefinition minLogLevelACS = AcsLogLevelDefinition.fromXsdLogLevel(logConfig.getNamedLoggerConfig(loggerName).getMinLogLevelLocal());
        setLevel(AcsLogLevel.getLowestMatchingJdkLevel(minLogLevelACS));
    } catch (Exception ex) {
        publish(new LogRecord(Level.WARNING, "Failed to configure stdout log handler: " + ex.toString()));
    }
}
Also used : LogRecord(java.util.logging.LogRecord) AcsLogLevelDefinition(alma.acs.logging.level.AcsLogLevelDefinition)

Aggregations

AcsLogLevelDefinition (alma.acs.logging.level.AcsLogLevelDefinition)21 AcsLogLevel (alma.acs.logging.AcsLogLevel)4 LogConfig (alma.acs.logging.config.LogConfig)3 UnnamedLogger (alma.maci.loggingconfig.UnnamedLogger)3 LogRecord (java.util.logging.LogRecord)3 AcsLogRecord (alma.acs.logging.AcsLogRecord)2 LogParameterUtil (alma.acs.logging.LogParameterUtil)2 LogReceiver (alma.acs.logging.engine.LogReceiver)2 StopWatch (alma.acs.util.StopWatch)2 AlarmTestComponent (alma.alarmContainerTest.AlarmTestComponent)2 LoggingConfig (alma.maci.loggingconfig.LoggingConfig)2 LogTypeHelper (com.cosylab.logging.engine.log.LogTypeHelper)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 Date (java.util.Date)2 Map (java.util.Map)2 Level (java.util.logging.Level)2 Test (org.junit.Test)2 ContainerLogLevelSpec (acs.benchmark.util.ContainerUtil.ContainerLogLevelSpec)1 ACSComponentOperations (alma.ACS.ACSComponentOperations)1