Search in sources :

Example 1 with AcsXMLLogFormatter

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

the class LocalOnlyAcsLogger method getInstance.

public static synchronized LocalOnlyAcsLogger getInstance(String namespace, Level level) {
    if (instance == null) {
        LogConfig testLogConfig = new LogConfig();
        testLogConfig.setDefaultMinLogLevelLocal(AcsLogLevel.getNativeLevel(level).getAcsLevel());
        instance = new LocalOnlyAcsLogger(namespace, testLogConfig);
        instance.setUseParentHandlers(false);
        Handler logHandler = new StdOutConsoleHandler(testLogConfig, namespace, null);
        logHandler.setFormatter(new AcsXMLLogFormatter() {

            public String format(LogRecord lr) {
                String xml = super.format(lr);
                return xml + '\n';
            }
        });
        logHandler.setLevel(level);
        instance.addHandler(logHandler);
    }
    return instance;
}
Also used : AcsXMLLogFormatter(alma.acs.logging.formatters.AcsXMLLogFormatter) LogRecord(java.util.logging.LogRecord) Handler(java.util.logging.Handler) LogConfig(alma.acs.logging.config.LogConfig)

Example 2 with AcsXMLLogFormatter

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

the class ClientLogManager method internalInitRemoteLogging.

/**
     * {@link #initRemoteLogging(ORB, Manager, int, boolean)} and {@link #initRemoteLoggingForService(ORB, boolean)}
     * delegate to this method for the initialization of the remote logging.
     * <P>
     * If the passed manager is <code>null</code>, <code>internalInitRemoteLogging</code> gets
     * the log service reference from the naming service; if the manager is not <code>null</code>,
     * it asked the manager for the reference to the log service.
     * 
     * @param orb The not <code>null</code> ORB
     * @param manager the ACS manager (it can be <code>null</code>)
     * @param managerHandle the handle assigned by the ACS Manager for this client;
     *                      ignored if the manager is <code>null</code>
     * @param retry if <code>true</code>, a failing connection to the log service will trigger up to 5 other attempts, every 10 seconds.
     * @return <code>true</code> if remote logging was initialized successfully
     */
private boolean internalInitRemoteLogging(ORB orb, Manager manager, int managerHandle, boolean retry) {
    if (logDispatcher != null) {
        System.out.println("Ignoring call to ClientLogManager#init: already initialized!");
        // todo: or is there a case where we want to retrieve the log service again? 
        return false;
    }
    if (orb == null) {
        System.out.println("Given ORB is null.");
        return false;
    }
    if (manager == null) {
        System.out.println("Will retrieve the log service reference from the naming service...");
    } else if (managerHandle <= 0) {
        System.out.println("can't connect to log service: invalid handle " + managerHandle);
        return false;
    }
    AcsLogServiceOperations logService = null;
    int count = 0;
    String errMsg;
    do {
        errMsg = null;
        count++;
        try {
            // normally there will be a remote handler and log queue already, which has captured all log records produced so far.
            // However, if suppressRemoteLogging was called, we need to set up remote logging from scratch. 
            prepareRemoteLogging();
            if (manager != null) {
                logService = getLogService(manager, managerHandle);
            } else {
                logService = getLogServiceFromNameSarvice(orb);
            }
            if (logService == null) {
                errMsg = "Failed to obtain central log service '" + logServiceName + "': reference is 'null'. ";
            } else {
                // we keep the above get_service call outside this locked section in order to not block shutdown() too long
                logQueueLock.lock();
                //System.out.println("ClientLogManager#initRemoteLogging got the logQueue lock");
                try {
                    if (logQueue == null) {
                        // this can happen if shutdown or suppressRemoteLogging is called concurrently
                        System.out.println("Will abort ClientLogManager#initRemoteLogging because remote logging seems no longer needed.");
                        return false;
                    }
                    if (count > 1) {
                        // all is fine, but we report the difficulty
                        m_internalLogger.info("Connected to central log service after initial failure. ");
                    }
                    // make log service available to our dispatcher, and flush the records collected so far
                    if (LOG_BIN_TYPE) {
                        logDispatcher = new RemoteLogDispatcher(orb, logService, new AcsBinLogFormatter());
                    } else {
                        logDispatcher = new RemoteLogDispatcher(orb, logService, new AcsXMLLogFormatter());
                    }
                    logQueue.setRemoteLogDispatcher(logDispatcher);
                    logQueue.flushAllAndWait();
                    logQueue.setPeriodicFlushing(flushPeriodSeconds * 1000);
                } finally {
                    logQueueLock.unlock();
                //	                	System.out.println("ClientLogManager#initRemoteLogging released the logQueue lock");
                }
            }
        } catch (Throwable thr) {
            errMsg = "Failed to connect to central log service with exception " + thr.toString();
            // as this message must go repeatedly to the command line output regardless of local log level, 
            // we don't want to print the multi-line exception stack, but just the original location.
            StackTraceElement[] trace = thr.getStackTrace();
            if (trace != null && trace.length > 0) {
                StackTraceElement traceOrigin = trace[0];
                errMsg += " in file " + traceOrigin.getFileName() + ", line " + traceOrigin.getLineNumber();
            }
            errMsg += ". ";
        }
        if (errMsg != null) {
            // can't use the loggers, so println is ok here
            if (retry) {
                System.out.println(errMsg + "Will try again in 10 seconds.");
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    System.out.println("Abandoning ClientLogManager#initRemoteLogging retries because of thread interruption.");
                    retry = false;
                }
            } else {
                System.out.println(errMsg);
            }
        }
    } while (retry && count <= 5 && errMsg != null);
    return (errMsg == null);
}
Also used : AcsLogServiceOperations(alma.Logging.AcsLogServiceOperations) AcsXMLLogFormatter(alma.acs.logging.formatters.AcsXMLLogFormatter) AcsBinLogFormatter(alma.acs.logging.formatters.AcsBinLogFormatter)

Example 3 with AcsXMLLogFormatter

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

the class RemoteLogDispatcher method sendLogRecords.

/**
     * Attempts to send <code>logRecords</code> over to the remote logging service.
     * To not lose any log records in case of failure, they can be obtained from the returned 
     * <code>FailedLogRecords</code> object, and should be fed back to the log record queue in order to try again later.
     * <p>
     * Should not be called concurrently (which can't happen since we use a single threaded executor 
     * in <code>DispatchingLogQueue</code>).
     * <p>
     * Sorts all log records by timestamp before converting them for remote transmission.
     * 
     * @param logRecords
     * @return those LogRecords that failed to be sent, either because they could not be converted to Any, 
     * or because the remote logger failed.
     */
FailedLogRecords sendLogRecords(LogRecord[] logRecords) {
    // sort this set of records by timestamp (queue delivers them by level/timestamp)
    Arrays.sort(logRecords, timestampLogRecordComparator);
    FailedLogRecords failures = new FailedLogRecords();
    // used for feeding back these LogRecords if the sending fails
    List<LogRecord> candidateLogRecords = new ArrayList<LogRecord>();
    if (useAcsLogServiceExtensions) {
        // create CORBA XmlLogRecord containing XML representations of log records and the log level as a separate field
        List<XmlLogRecord> remoteLogRecords = new ArrayList<XmlLogRecord>();
        for (int i = 0; i < logRecords.length; i++) {
            if (i < getBufferSize()) {
                try {
                    String xml = ((AcsXMLLogFormatter) logFormatter).format(logRecords[i]);
                    int level = AcsLogLevel.getNativeLevel(logRecords[i].getLevel()).getAcsLevel().value;
                    XmlLogRecord remoteLogRecord = new XmlLogRecord(xml, (short) level);
                    remoteLogRecords.add(remoteLogRecord);
                    candidateLogRecords.add(logRecords[i]);
                } catch (RuntimeException e) {
                    failures.addSerializationFailure(logRecords[i]);
                }
            } else {
                // records that don't fit into one remote call must be sent later
                // this should never happen except for during concurrently changing buffer size.
                failures.addSendFailure(logRecords[i]);
            }
        }
        // send the log records over CORBA
        if (!remoteLogRecords.isEmpty()) {
            XmlLogRecord[] remoteLogRecordsArray = remoteLogRecords.toArray(new XmlLogRecord[remoteLogRecords.size()]);
            writeRecords(remoteLogRecordsArray);
        }
    } else {
        // default is Corba telecom Log interface that requires records inside Anys
        List<Any> anyLogRecords = new ArrayList<Any>();
        for (int i = 0; i < logRecords.length; i++) {
            if (i < getBufferSize()) {
                try {
                    Any anyLogRecord = orb.create_any();
                    anyLogRecord = logFormatter.formatAny(anyLogRecord, logRecords[i]);
                    anyLogRecords.add(anyLogRecord);
                    candidateLogRecords.add(logRecords[i]);
                } catch (RuntimeException e) {
                    failures.addSerializationFailure(logRecords[i]);
                }
            } else {
                // records that don't fit into one remote call must be sent later
                failures.addSendFailure(logRecords[i]);
            }
        }
        // send the log records over CORBA 
        if (!anyLogRecords.isEmpty()) {
            Any[] anyLogRecordsArray = anyLogRecords.toArray(new Any[anyLogRecords.size()]);
            try {
                writeRecords(anyLogRecordsArray);
            }//            // LogOffDuty - other exotic reasons to be not "on duty", such as log duration time or scheduling time
             catch (Throwable thr) {
                // feed back these records to we can try them again later
                failures.addSendFailures(candidateLogRecords);
            // Currently ACS does not make use of the semantics of the various exceptions specified here by CORBA, i.e.
            // LogDisabled, LogOffDuty, LogLocked, LogFull (see above)
            // There can also be java.net.ConnectException thrown.. @TODO perhaps we should print that to stdout, with repeatGuard.
            }
        }
    }
    return failures;
}
Also used : AcsXMLLogFormatter(alma.acs.logging.formatters.AcsXMLLogFormatter) ArrayList(java.util.ArrayList) Any(org.omg.CORBA.Any) XmlLogRecord(alma.Logging.XmlLogRecord) LogRecord(java.util.logging.LogRecord) XmlLogRecord(alma.Logging.XmlLogRecord)

Aggregations

AcsXMLLogFormatter (alma.acs.logging.formatters.AcsXMLLogFormatter)3 LogRecord (java.util.logging.LogRecord)2 AcsLogServiceOperations (alma.Logging.AcsLogServiceOperations)1 XmlLogRecord (alma.Logging.XmlLogRecord)1 LogConfig (alma.acs.logging.config.LogConfig)1 AcsBinLogFormatter (alma.acs.logging.formatters.AcsBinLogFormatter)1 ArrayList (java.util.ArrayList)1 Handler (java.util.logging.Handler)1 Any (org.omg.CORBA.Any)1