Search in sources :

Example 1 with LogBinaryRecord

use of alma.ACSLoggingLog.LogBinaryRecord 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 LogBinaryRecord

use of alma.ACSLoggingLog.LogBinaryRecord in project ACS by ACS-Community.

the class AnyLogTest method packBinLogs.

static double packBinLogs() {
    LogBinaryRecord s_log = new LogBinaryRecord();
    // source info
    s_log.type = (short) AcsLogLevelDefinition.INFO.value;
    s_log.TimeStamp = "2007-12-12";
    s_log.File = "filename";
    s_log.Line = 21;
    s_log.Routine = "routine";
    s_log.Host = "host";
    s_log.Process = "process";
    s_log.Thread = "thread";
    s_log.LogContext = "Context";
    s_log.SourceObject = "SourceObject";
    s_log.Audience = "Audience";
    s_log.StackId = "Stackid";
    s_log.StackLevel = 8;
    s_log.LogId = "LogId";
    s_log.Uri = "Uri";
    s_log.Priority = 1;
    NameValue[] aux = new NameValue[2];
    aux[0] = new NameValue("name1", "value1");
    aux[1] = new NameValue("name2", "value2");
    int j;
    s_log.log_data = new NameValue[2];
    s_log.attributes = new NameValue[0];
    for (j = 0; j < 2; j++) {
        s_log.log_data[j] = aux[j];
    }
    s_log.MsgData = "I'm a message";
    Date start = new Date();
    LogBinaryRecordHelper.insert(bin_record, s_log);
    Date end = new Date();
    double elapsed = ((double) (end.getTime() - start.getTime()));
    return elapsed;
}
Also used : NameValue(alma.ACSLoggingLog.NameValue) LogBinaryRecord(alma.ACSLoggingLog.LogBinaryRecord) Date(java.util.Date)

Example 3 with LogBinaryRecord

use of alma.ACSLoggingLog.LogBinaryRecord in project ACS by ACS-Community.

the class LogDispatcherTest method toCacheString.

/**
	 * Build the string for the cache out of an ILogEntry for the case of
	 * binary logs
	 * 
	 * @param log The log to get the cache string in binary format
	 * @return The cache string (binary format)
	 */
private synchronized String toCacheString(ILogEntry log) throws Exception {
    LogBinaryRecord logBin = convertLogToBinary(log);
    String str = null;
    try {
        str = com.cosylab.logging.engine.ACS.CacheUtils.toCacheString(logBin);
    } catch (Throwable t) {
        t.printStackTrace(System.err);
        System.err.println("Log with error " + logBin.toString());
    }
    return str;
}
Also used : LogBinaryRecord(alma.ACSLoggingLog.LogBinaryRecord)

Example 4 with LogBinaryRecord

use of alma.ACSLoggingLog.LogBinaryRecord in project ACS by ACS-Community.

the class LogDispatcherTest method convertLogToBinary.

/**
	 * Convert a ILogEntry into a binary log
	 * 
	 * @param The log to convert
	 * @return The binary log
	 * 
	 */
private LogBinaryRecord convertLogToBinary(ILogEntry log) throws Exception {
    LogBinaryRecord logBin = new LogBinaryRecord();
    logBin.Audience = (String) log.getField(LogField.AUDIENCE);
    logBin.File = (String) log.getField(LogField.FILE);
    logBin.Host = (String) log.getField(LogField.HOST);
    Integer line = (Integer) log.getField(LogField.LINE);
    if (line != null) {
        logBin.Line = line;
    } else {
        logBin.Line = 0;
    }
    logBin.LogContext = (String) log.getField(LogField.CONTEXT);
    logBin.LogId = (String) log.getField(LogField.LOGID);
    logBin.MsgData = (String) log.getField(LogField.LOGMESSAGE);
    Integer priority = (Integer) log.getField(LogField.PRIORITY);
    if (priority != null) {
        logBin.Priority = priority;
    } else {
        logBin.Priority = 0;
    }
    logBin.Process = (String) log.getField(LogField.PROCESS);
    logBin.Routine = (String) log.getField(LogField.ROUTINE);
    logBin.SourceObject = (String) log.getField(LogField.SOURCEOBJECT);
    logBin.StackId = (String) log.getField(LogField.STACKID);
    Integer stackL = (Integer) log.getField(LogField.STACKLEVEL);
    if (stackL != null) {
        logBin.StackLevel = stackL;
    } else {
        logBin.StackLevel = 0;
    }
    logBin.Thread = (String) log.getField(LogField.THREAD);
    final Date date = new Date((Long) log.getField(LogField.TIMESTAMP));
    logBin.TimeStamp = IsoDateFormat.formatDate(date);
    logBin.type = (short) log.getType().ordinal();
    logBin.Uri = (String) log.getField(LogField.URI);
    if (log.hasDatas()) {
        Vector<AdditionalData> data = log.getAdditionalData();
        logBin.log_data = new NameValue[data.size()];
        for (int t = 0; t < data.size(); t++) {
            AdditionalData d = data.get(t);
            logBin.log_data[t] = new NameValue(d.name, d.value);
        }
    }
    return logBin;
}
Also used : AdditionalData(com.cosylab.logging.engine.log.ILogEntry.AdditionalData) NameValue(alma.ACSLoggingLog.NameValue) LogBinaryRecord(alma.ACSLoggingLog.LogBinaryRecord) Date(java.util.Date)

Example 5 with LogBinaryRecord

use of alma.ACSLoggingLog.LogBinaryRecord in project ACS by ACS-Community.

the class AnyLogTest method unpackBinLogs.

static double unpackBinLogs() {
    Date start = new Date();
    LogBinaryRecord logRecord = LogBinaryRecordHelper.extract(bin_record);
    Date end = new Date();
    double elapsed = ((double) (end.getTime() - start.getTime()));
    return elapsed;
}
Also used : LogBinaryRecord(alma.ACSLoggingLog.LogBinaryRecord) Date(java.util.Date)

Aggregations

LogBinaryRecord (alma.ACSLoggingLog.LogBinaryRecord)6 Date (java.util.Date)4 NameValue (alma.ACSLoggingLog.NameValue)3 AcsLogLevel (alma.acs.logging.AcsLogLevel)1 AcsLogRecord (alma.acs.logging.AcsLogRecord)1 LogParameterUtil (alma.acs.logging.LogParameterUtil)1 AcsLogLevelDefinition (alma.acs.logging.level.AcsLogLevelDefinition)1 AdditionalData (com.cosylab.logging.engine.log.ILogEntry.AdditionalData)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Map (java.util.Map)1