Search in sources :

Example 1 with LogParameterUtil

use of alma.acs.logging.LogParameterUtil 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 2 with LogParameterUtil

use of alma.acs.logging.LogParameterUtil 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 3 with LogParameterUtil

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

the class ConsoleLogFormatter method format.

/**
	 * Format the given LogRecord.
	 * @param record the log record to be formatted.
	 * @return a formatted log record
	 */
public String format(LogRecord record) {
    StringBuffer sb = new StringBuffer(128);
    synchronized (date) {
        date.setTime(record.getMillis());
        sb.append(IsoDateFormat.formatDate(date));
    }
    sb.append(' ');
    sb.append(record.getLevel().getLocalizedName());
    String sourceObject = record.getLoggerName();
    if (sourceObject != null) {
        sb.append(" [").append(sourceObject).append("] ");
    }
    sb.append(record.getMessage());
    // log parameters (except for the special properties which were used already to set specific fields)
    // @TODO check if analyzing the parameters both here and in AcsXMLLogFormatter uses lots of CPU and thus should be unified.
    //       This would probably require us to either use only one log handler for remote and stdout log,
    //       or to no longer stuff all parameters and other extra data into maps that become log parameters,
    //       which allows us to deal with standard LogRecords and not just custom AcsLogRecords.
    LogParameterUtil logParamUtil = new LogParameterUtil(record);
    String paramString = "";
    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 = propertiesMap.get(keyName).toString();
                paramString += keyName + "='" + 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();
            paramString += "LoggedParameter" + "='" + value + "' ";
        }
    }
    if (!paramString.isEmpty()) {
        sb.append(" [ " + paramString + "]");
    }
    sb.append(lineSeparator);
    // exceptions
    if (record.getThrown() != null) {
        try {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            record.getThrown().printStackTrace(pw);
            pw.close();
            sb.append(sw.toString());
        } catch (Exception ex) {
        // just ignore it
        }
    }
    return new String(sb);
}
Also used : StringWriter(java.io.StringWriter) LogParameterUtil(alma.acs.logging.LogParameterUtil) Map(java.util.Map) PrintWriter(java.io.PrintWriter)

Aggregations

LogParameterUtil (alma.acs.logging.LogParameterUtil)3 PrintWriter (java.io.PrintWriter)3 StringWriter (java.io.StringWriter)3 Map (java.util.Map)3 AcsLogLevel (alma.acs.logging.AcsLogLevel)2 AcsLogRecord (alma.acs.logging.AcsLogRecord)2 AcsLogLevelDefinition (alma.acs.logging.level.AcsLogLevelDefinition)2 Date (java.util.Date)2 LogBinaryRecord (alma.ACSLoggingLog.LogBinaryRecord)1 NameValue (alma.ACSLoggingLog.NameValue)1