Search in sources :

Example 1 with AccessLogData

use of org.apache.dubbo.rpc.support.AccessLogData in project dubbo by alibaba.

the class AccessLogFilterTest method testDefault.

// TODO how to assert thread action
@Test
@SuppressWarnings("unchecked")
public void testDefault() throws NoSuchFieldException, IllegalAccessException {
    URL url = URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1");
    Invoker<AccessLogFilterTest> invoker = new MyInvoker<AccessLogFilterTest>(url);
    Invocation invocation = new MockInvocation();
    Field field = AccessLogFilter.class.getDeclaredField("LOG_ENTRIES");
    ReflectUtils.makeAccessible(field);
    assertTrue(((Map) field.get(AccessLogFilter.class)).isEmpty());
    accessLogFilter.invoke(invoker, invocation);
    Map<String, Queue<AccessLogData>> logs = (Map<String, Queue<AccessLogData>>) field.get(AccessLogFilter.class);
    assertFalse(logs.isEmpty());
    assertFalse(logs.get("true").isEmpty());
    AccessLogData log = logs.get("true").iterator().next();
    assertEquals("org.apache.dubbo.rpc.support.DemoService", log.getServiceName());
}
Also used : Invocation(org.apache.dubbo.rpc.Invocation) MockInvocation(org.apache.dubbo.rpc.support.MockInvocation) URL(org.apache.dubbo.common.URL) Field(java.lang.reflect.Field) MockInvocation(org.apache.dubbo.rpc.support.MockInvocation) MyInvoker(org.apache.dubbo.rpc.support.MyInvoker) AccessLogData(org.apache.dubbo.rpc.support.AccessLogData) Queue(java.util.Queue) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 2 with AccessLogData

use of org.apache.dubbo.rpc.support.AccessLogData in project dubbo by alibaba.

the class AccessLogFilter method invoke.

/**
 * This method logs the access log for service method invocation call.
 *
 * @param invoker service
 * @param inv     Invocation service method.
 * @return Result from service method.
 * @throws RpcException
 */
@Override
public Result invoke(Invoker<?> invoker, Invocation inv) throws RpcException {
    try {
        String accessLogKey = invoker.getUrl().getParameter(ACCESS_LOG_KEY);
        if (ConfigUtils.isNotEmpty(accessLogKey)) {
            AccessLogData logData = AccessLogData.newLogData();
            logData.buildAccessLogData(invoker, inv);
            log(accessLogKey, logData);
        }
    } catch (Throwable t) {
        logger.warn("Exception in AccessLogFilter of service(" + invoker + " -> " + inv + ")", t);
    }
    return invoker.invoke(inv);
}
Also used : AccessLogData(org.apache.dubbo.rpc.support.AccessLogData)

Example 3 with AccessLogData

use of org.apache.dubbo.rpc.support.AccessLogData in project dubbo by alibaba.

the class AccessLogFilter method processWithServiceLogger.

private void processWithServiceLogger(Queue<AccessLogData> logQueue) {
    for (Iterator<AccessLogData> iterator = logQueue.iterator(); iterator.hasNext(); iterator.remove()) {
        AccessLogData logData = iterator.next();
        LoggerFactory.getLogger(LOG_KEY + "." + logData.getServiceName()).info(logData.getLogMessage());
    }
}
Also used : AccessLogData(org.apache.dubbo.rpc.support.AccessLogData)

Example 4 with AccessLogData

use of org.apache.dubbo.rpc.support.AccessLogData in project dubbo by alibaba.

the class AccessLogFilter method processWithAccessKeyLogger.

private void processWithAccessKeyLogger(Queue<AccessLogData> logQueue, File file) throws IOException {
    try (FileWriter writer = new FileWriter(file, true)) {
        for (Iterator<AccessLogData> iterator = logQueue.iterator(); iterator.hasNext(); iterator.remove()) {
            writer.write(iterator.next().getLogMessage());
            writer.write(System.getProperty(LINE_SEPARATOR));
        }
        writer.flush();
    }
}
Also used : FileWriter(java.io.FileWriter) AccessLogData(org.apache.dubbo.rpc.support.AccessLogData)

Aggregations

AccessLogData (org.apache.dubbo.rpc.support.AccessLogData)4 FileWriter (java.io.FileWriter)1 Field (java.lang.reflect.Field)1 Map (java.util.Map)1 Queue (java.util.Queue)1 URL (org.apache.dubbo.common.URL)1 Invocation (org.apache.dubbo.rpc.Invocation)1 MockInvocation (org.apache.dubbo.rpc.support.MockInvocation)1 MyInvoker (org.apache.dubbo.rpc.support.MyInvoker)1 Test (org.junit.jupiter.api.Test)1