Search in sources :

Example 1 with LogLineTableEntity

use of com.peterphi.servicemanager.service.logging.LogLineTableEntity in project stdlib by petergeneric.

the class LogSerialiser method toJson.

public String toJson(final Collection<LogLineTableEntity> results) {
    try {
        StringWriter sw = new StringWriter(4096);
        JSONWriter w = new JSONWriter(sw);
        w.array();
        for (LogLineTableEntity line : results) write(w, line);
        w.endArray();
        return sw.toString();
    } catch (JSONException e) {
        throw new RuntimeException("Error serialising lines to JSON: " + e.getMessage(), e);
    }
}
Also used : JSONWriter(org.codehaus.jettison.json.JSONWriter) StringWriter(java.io.StringWriter) LogLineTableEntity(com.peterphi.servicemanager.service.logging.LogLineTableEntity) JSONException(org.codehaus.jettison.json.JSONException)

Example 2 with LogLineTableEntity

use of com.peterphi.servicemanager.service.logging.LogLineTableEntity in project stdlib by petergeneric.

the class AzureLogStore method storeAsBatch.

@Retry
public void storeAsBatch(final List<LogLineTableEntity> lines) throws StorageException {
    if (lines.isEmpty())
        // nothing to do!
        return;
    TableBatchOperation operation = new TableBatchOperation();
    for (LogLineTableEntity line : lines) {
        if (log.isTraceEnabled())
            log.trace("Storing entity with partition=" + line.getPartitionKey() + ", row=" + line.getRowKey());
        operation.insertOrReplace(line);
    }
    logdata.execute(operation);
}
Also used : LogLineTableEntity(com.peterphi.servicemanager.service.logging.LogLineTableEntity) TableBatchOperation(com.microsoft.azure.storage.table.TableBatchOperation) Retry(com.peterphi.std.guice.common.retry.annotation.Retry)

Example 3 with LogLineTableEntity

use of com.peterphi.servicemanager.service.logging.LogLineTableEntity in project stdlib by petergeneric.

the class FileLogStore method store.

@Override
public synchronized void store(List<LogLineTableEntity> lines) throws RuntimeException {
    if (System.currentTimeMillis() > timestampForNextFile) {
        // Close the existing file
        IOUtils.closeQuietly(currentFile);
        currentFile = null;
        try {
            FileWriter fw = new FileWriter(new File(storePath, LocalDate.now().toString() + ".log"), true);
            currentFile = fw;
        } catch (IOException e) {
            throw new RuntimeException("Error opening new log file! " + e.getMessage(), e);
        }
        // Get the timestamp for the start of tomorrow
        timestampForNextFile = LocalDate.now().plusDays(1).toDateTimeAtStartOfDay().getMillis();
    }
    // Now append all the logs
    for (LogLineTableEntity line : lines) {
        try {
            currentFile.append(format(line));
        } catch (IOException e) {
            throw new RuntimeException("Error appending log line " + line + " to log file! " + e.getMessage(), e);
        }
    }
    try {
        currentFile.flush();
    } catch (IOException e) {
    // ignore
    }
}
Also used : LogLineTableEntity(com.peterphi.servicemanager.service.logging.LogLineTableEntity) FileWriter(java.io.FileWriter) IOException(java.io.IOException) File(java.io.File)

Example 4 with LogLineTableEntity

use of com.peterphi.servicemanager.service.logging.LogLineTableEntity in project stdlib by petergeneric.

the class ServiceManagerUIServiceImpl method doSearchLogs.

@Override
public Response doSearchLogs(String fromStr, String toStr, String filter) {
    // TODO take into account the user's timezone!
    DateTime from = DateTime.parse(fromStr).withZone(LONDON);
    // TODO take into account the user's timezone!
    DateTime to = DateTime.parse(fromStr).withZone(LONDON);
    final List<LogLineTableEntity> results = logStore.search(from, to, filter);
    return Response.ok().type(MediaType.APPLICATION_JSON_TYPE).entity(serialiser.toJson(results)).build();
}
Also used : LogLineTableEntity(com.peterphi.servicemanager.service.logging.LogLineTableEntity) DateTime(org.joda.time.DateTime)

Example 5 with LogLineTableEntity

use of com.peterphi.servicemanager.service.logging.LogLineTableEntity in project stdlib by petergeneric.

the class ServiceManagerLoggingRestServiceImpl method report.

@Override
public void report(final LogReport logs) {
    if (log.isTraceEnabled())
        log.trace("Received " + logs.getLines().length + " log lines to store");
    try {
        final ServiceInstanceEntity serviceInstance = cache.get(logs.getServiceId());
        // Make sure that all store calls are for the same partition (date + instance id)
        // This is technically only a requirement for Azure but it's a guarantee other
        // stores can use effectively (e.g. writing to datestamped log files)
        String partitionKey = null;
        List<LogLineTableEntity> pending = new ArrayList<>();
        for (LogLine line : logs.getLines()) {
            LogLineTableEntity entity = convert(serviceInstance, line);
            if (partitionKey == null) {
                // First entry in a new partition
                partitionKey = entity.getPartitionKey();
            } else if (!partitionKey.equals(entity.getPartitionKey())) {
                // Flush all the lines up til now and then start a new list
                service.store(pending);
                pending = new ArrayList<>();
                partitionKey = entity.getPartitionKey();
            }
            pending.add(entity);
        }
        // Make sure we flush any remaining data to the storage system
        service.store(pending);
    } catch (Throwable t) {
        log.error("Error saving logs", t);
        throw t;
    }
}
Also used : LogLineTableEntity(com.peterphi.servicemanager.service.logging.LogLineTableEntity) ArrayList(java.util.ArrayList) ServiceInstanceEntity(com.peterphi.servicemanager.service.db.entity.ServiceInstanceEntity) LogLine(com.peterphi.std.guice.common.logging.logreport.LogLine)

Aggregations

LogLineTableEntity (com.peterphi.servicemanager.service.logging.LogLineTableEntity)5 TableBatchOperation (com.microsoft.azure.storage.table.TableBatchOperation)1 ServiceInstanceEntity (com.peterphi.servicemanager.service.db.entity.ServiceInstanceEntity)1 LogLine (com.peterphi.std.guice.common.logging.logreport.LogLine)1 Retry (com.peterphi.std.guice.common.retry.annotation.Retry)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 JSONException (org.codehaus.jettison.json.JSONException)1 JSONWriter (org.codehaus.jettison.json.JSONWriter)1 DateTime (org.joda.time.DateTime)1