Search in sources :

Example 1 with LogLine

use of com.google.apphosting.api.logservice.LogServicePb.LogLine in project appengine-java-standard by GoogleCloudPlatform.

the class LocalLogServiceTest method getTestData.

List<RequestLog> getTestData(long start, String version, boolean appLogsDesired, boolean complete) {
    List<RequestLog> list = new ArrayList<>();
    for (int i = 0; i < 20; i++) {
        Long end;
        if (complete) {
            end = start + i + 1;
        } else {
            end = null;
        }
        RequestLog.Builder rl = getLog(start + i, end, version).toBuilder();
        if (appLogsDesired) {
            LogLine line = LogLine.newBuilder().setTime(i).setLevel(i % 5).setLogMessage(Integer.toString(i)).build();
            rl.addLine(line);
        }
        list.add(rl.build());
    }
    return list;
}
Also used : RequestLog(com.google.apphosting.api.logservice.LogServicePb.RequestLog) ArrayList(java.util.ArrayList) LogLine(com.google.apphosting.api.logservice.LogServicePb.LogLine) AppLogLine(com.google.appengine.api.log.AppLogLine)

Example 2 with LogLine

use of com.google.apphosting.api.logservice.LogServicePb.LogLine in project appengine-java-standard by GoogleCloudPlatform.

the class LocalLogServiceTest method writeTestData.

void writeTestData(List<RequestLog> data, boolean complete) {
    for (RequestLog log : data) {
        String requestId = Long.toString(log.getStartTime());
        LocalLogService logService = getLocalLogService();
        logService.registerResponseSize(log.getResponseSize());
        logService.addRequestInfo(log.getAppId(), log.getVersionId(), requestId, log.getIp(), log.getNickname(), log.getStartTime(), log.getEndTime(), log.getMethod(), log.getResource(), log.getHttpVersion(), log.getUserAgent(), complete, 200, null);
        logService.clearResponseSize();
        for (LogLine line : log.getLineList()) {
            Level level = Level.parse(Integer.toString(line.getLevel()));
            logService.addAppLogLine(requestId, line.getTime(), level.intValue(), line.getLogMessage());
        }
    }
}
Also used : RequestLog(com.google.apphosting.api.logservice.LogServicePb.RequestLog) Level(java.util.logging.Level) LogLevel(com.google.appengine.api.log.LogService.LogLevel) ByteString(com.google.protobuf.ByteString) LogLine(com.google.apphosting.api.logservice.LogServicePb.LogLine) AppLogLine(com.google.appengine.api.log.AppLogLine)

Example 3 with LogLine

use of com.google.apphosting.api.logservice.LogServicePb.LogLine in project appengine-java-standard by GoogleCloudPlatform.

the class LocalLogService method addAppLogLine.

public synchronized void addAppLogLine(String requestId, long time, int level, String message) {
    if (message == null) {
        // ignore requests to log null messages
        return;
    }
    LogLine line = LogLine.newBuilder().setTime(time).setLevel(level).setLogMessage(message).build();
    int index = logs.indexOf(findLogInLogMapOrAddNewLog(requestId));
    RequestLog log = findLogInLogMapOrAddNewLog(requestId).toBuilder().addLine(line).buildPartial();
    // Replace element in the existing list, while keeping position.
    logs.set(index, log);
}
Also used : RequestLog(com.google.apphosting.api.logservice.LogServicePb.RequestLog) LogLine(com.google.apphosting.api.logservice.LogServicePb.LogLine)

Example 4 with LogLine

use of com.google.apphosting.api.logservice.LogServicePb.LogLine in project appengine-java-standard by GoogleCloudPlatform.

the class LocalLogService method read.

/**
 * Reads log records from the in-memory log list and applies user-specified filters to the results
 * to return.
 *
 * @param request A set of parameters that indicate restrictions on the results that should be
 *     returned.
 * @return A set of logs matching the parameters given. If the number of logs returned exceed
 *     either the user-specified amount or the API-specified limit, then an offset is returned
 *     that has a reference to the next record to read from in subsequent requests.
 */
public synchronized LogReadResponse read(Status status, LogReadRequest request) {
    LogReadResponse.Builder response = LogReadResponse.newBuilder();
    Integer index = 0;
    Set<ByteString> requestedIds = null;
    if (!request.getRequestIdList().isEmpty()) {
        requestedIds = new HashSet<>(request.getRequestIdList());
    }
    // after this one is acceptable.
    if (request.hasOffset()) {
        index = null;
        BigInteger requestToFind = new BigInteger(request.getOffset().getRequestId().toStringUtf8(), 16);
        for (int i = 0; i < logs.size(); i++) {
            BigInteger thisRequestId = new BigInteger(logs.get(i).getRequestId().toStringUtf8(), 16);
            if (requestToFind.compareTo(thisRequestId) > 0) {
                index = i;
                break;
            }
        }
        // that they don't ask us for any more logs.
        if (index == null) {
            return response.build();
        }
    }
    int numResultsFetched = 0;
    for (int i = index; i < logs.size(); i++) {
        RequestLog thisLog = null;
        int j = 0;
        for (RequestLog log : logs) {
            if (i == j) {
                thisLog = log;
                break;
            }
            j++;
        }
        if (requestedIds != null && !requestedIds.contains(thisLog.getRequestId())) {
            continue;
        }
        // time) against the provided start/end times.
        if (request.hasStartTime()) {
            if (request.getStartTime() > thisLog.getEndTime()) {
                continue;
            }
        }
        if (request.hasEndTime()) {
            if (request.getEndTime() <= thisLog.getEndTime()) {
                continue;
            }
        }
        // incomplete, don't include it.
        if (!request.getIncludeIncomplete() && !thisLog.getFinished()) {
            continue;
        }
        if (!request.getVersionIdList().isEmpty() && !request.getVersionIdList().contains(thisLog.getVersionId()) && thisLog.hasVersionId()) {
            continue;
        }
        if (!request.getModuleVersionList().isEmpty() && (thisLog.hasModuleId() || thisLog.hasVersionId())) {
            boolean moduleVersionMatch = false;
            for (LogModuleVersion moduleVersion : request.getModuleVersionList()) {
                if (thisLog.getModuleId().equals(moduleVersion.getModuleId()) && thisLog.getVersionId().equals(moduleVersion.getVersionId())) {
                    moduleVersionMatch = true;
                }
            }
            if (!moduleVersionMatch) {
                continue;
            }
        }
        if (request.hasMinimumLogLevel()) {
            // Find if there are any logs that meet or exceed minimumLogLevel.
            // If so (and if the user has specified that they want app logs), add
            // all the app logs to the response. If not, don't include this log in
            // the response.
            boolean logLevelMatched = false;
            for (LogLine line : thisLog.getLineList()) {
                if (line.getLevel() >= request.getMinimumLogLevel()) {
                    logLevelMatched = true;
                    break;
                }
            }
            if (!logLevelMatched) {
                continue;
            }
        }
        // At this point, the thisLog proto might only be partial, which is fine in dev mode
        // (difference between proto1 and proto2),
        // se we are filling mandatory fields with dummy data, so we do not need to change the
        // official
        // log service implementation.
        RequestLog.Builder logCopy = thisLog.toBuilder().clone();
        fillRequiredFields(logCopy);
        if (!request.getIncludeAppLogs()) {
            // If the user doesn't want app logs, make a copy of this log
            // that doesn't have that in it and give them that instead.
            logCopy.clearLine();
        }
        response.addLog(logCopy.build());
        numResultsFetched++;
        if (numResultsFetched >= request.getCount()) {
            // request id
            if (i + 1 < logs.size()) {
                ByteString nextOffset = logs.get(i).getRequestId();
                LogOffset offset = LogOffset.newBuilder().setRequestId(nextOffset).build();
                response.setOffset(offset);
            }
            break;
        }
    }
    return response.build();
}
Also used : ByteString(com.google.protobuf.ByteString) LogOffset(com.google.apphosting.api.logservice.LogServicePb.LogOffset) LogLine(com.google.apphosting.api.logservice.LogServicePb.LogLine) LogModuleVersion(com.google.apphosting.api.logservice.LogServicePb.LogModuleVersion) BigInteger(java.math.BigInteger) RequestLog(com.google.apphosting.api.logservice.LogServicePb.RequestLog) BigInteger(java.math.BigInteger) LogReadResponse(com.google.apphosting.api.logservice.LogServicePb.LogReadResponse)

Example 5 with LogLine

use of com.google.apphosting.api.logservice.LogServicePb.LogLine in project appengine-java-standard by GoogleCloudPlatform.

the class LocalLogServiceTest method testMinLogLevelFiltering.

@Test
public void testMinLogLevelFiltering() throws Exception {
    boolean appLogsDesired = true;
    List<RequestLog> allLogs = getTestData(defaultStartTime, defaultVersion, appLogsDesired, completeLogsRequested);
    writeTestData(allLogs, completeLogsRequested);
    List<Integer> allAppLogLevels = new ArrayList<>();
    for (RequestLog record : Lists.reverse(allLogs)) {
        for (LogLine line : record.getLineList()) {
            allAppLogLevels.add(line.getLevel());
        }
    }
    // Test filtering for all legitimate log level values
    for (LogLevel minLogLevel : LogLevel.values()) {
        int minLogLevelInt = minLogLevel.ordinal();
        List<Integer> expectedAppLogLevels = new ArrayList<>();
        for (Integer thisLevel : allAppLogLevels) {
            if (thisLevel >= minLogLevelInt) {
                expectedAppLogLevels.add(thisLevel);
            }
        }
        LogQuery query = LogQuery.Builder.withIncludeAppLogs(appLogsDesired).minLogLevel(minLogLevel);
        List<Integer> actualAppLogLevels = new ArrayList<>();
        for (RequestLogs record : logService.fetch(query)) {
            for (AppLogLine line : record.getAppLogLines()) {
                actualAppLogLevels.add(line.getLogLevel().ordinal());
            }
        }
        String expectedLevels = joinAppLogLevels(expectedAppLogLevels);
        String actualLevels = joinAppLogLevels(actualAppLogLevels);
        assertThat(actualLevels).isEqualTo(expectedLevels);
    }
}
Also used : ArrayList(java.util.ArrayList) LogLine(com.google.apphosting.api.logservice.LogServicePb.LogLine) AppLogLine(com.google.appengine.api.log.AppLogLine) ByteString(com.google.protobuf.ByteString) RequestLogs(com.google.appengine.api.log.RequestLogs) LogLevel(com.google.appengine.api.log.LogService.LogLevel) BigInteger(java.math.BigInteger) RequestLog(com.google.apphosting.api.logservice.LogServicePb.RequestLog) LogQuery(com.google.appengine.api.log.LogQuery) AppLogLine(com.google.appengine.api.log.AppLogLine) Test(org.junit.Test)

Aggregations

LogLine (com.google.apphosting.api.logservice.LogServicePb.LogLine)6 RequestLog (com.google.apphosting.api.logservice.LogServicePb.RequestLog)6 AppLogLine (com.google.appengine.api.log.AppLogLine)4 ByteString (com.google.protobuf.ByteString)4 BigInteger (java.math.BigInteger)3 ArrayList (java.util.ArrayList)3 LogQuery (com.google.appengine.api.log.LogQuery)2 LogLevel (com.google.appengine.api.log.LogService.LogLevel)2 RequestLogs (com.google.appengine.api.log.RequestLogs)2 Test (org.junit.Test)2 LogModuleVersion (com.google.apphosting.api.logservice.LogServicePb.LogModuleVersion)1 LogOffset (com.google.apphosting.api.logservice.LogServicePb.LogOffset)1 LogReadResponse (com.google.apphosting.api.logservice.LogServicePb.LogReadResponse)1 Level (java.util.logging.Level)1