tkowalcz / tjahzi

Java clients, log4j2 and logback appenders for Grafana Loki
MIT License
106 stars 18 forks source link

All sorts of lookups in labels don't work #85

Closed danieltaub closed 2 years ago

danieltaub commented 2 years ago

When trying the below configuration, the lookup donse't parse the lookup and just putting the lookup name as the label value. I'm using the appender latest version and applied all the configs as the docs.

using log4j2 via spring library, and run it as container in k8s. so the lookups should be valid, even copy the example from the docs dosen't work

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30" packages="pl.tkowalcz.tjahzi.log4j2">
    <Properties>
        <Proprty>
            <Property name="pod_name">${k8s:podName}</Property>
            <Property name="hostname">${k8s:hostname}</Property>
            <Property name="container_name">${k8s:containerName}</Property>
            <Property name="app">${sys:spring.application.name}</Property>
            <Property name="environment">${sys:spring.profiles.active}</Property>
        </Proprty>
    </Properties>
    <Appenders>
        <Console name="ConsoleAppender" target="SYSTEM_OUT" follow="true">
            <PatternLayout pattern="%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} %highlight{${LOG_LEVEL_PATTERN:-%5p}}{FATAL=red blink, ERROR=red, WARN=yellow bold, INFO=green, DEBUG=green bold, TRACE=blue} %style{${sys:PID}}{magenta} [%15.15t] %style{%-40.40C{1.}}{cyan} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>
        </Console>
        <Loki name="loki-appender">
            <host>loki.kube-prometheus-stack.svc.cluster.local</host>
            <port>3100</port>

            <PatternLayout>
                <Pattern>%X{tid} [%t] %d{MM-dd HH:mm:ss.SSS} %5p %c{1} - %m%n%exception{full}</Pattern>
            </PatternLayout>

            <Label name="app" value="${app}"/>
            <Label name="server" value="${sys:hostname}"/>
<!--            <Label name="server" value=${sys:hostname}/>-->
            <Label name="pod_name" value="${pod_name}"/>
            <Label name="hostname" value="${hostname}"/>
            <Label name="container_name" value="${container_name}"/>
            <Label name="environment" value="${environment}"/>
            <LogLevelLabel>level</LogLevelLabel>
        </Loki>

    </Appenders>
    <Loggers>
        <Root level="INFO">
            <AppenderRef ref="ConsoleAppender" />
            <AppenderRef ref="loki-appender"/>
        </Root>
        <Logger name="org.springframework" level="WARN" />
    </Loggers>
</Configuration>
tkowalcz commented 2 years ago

Hi,

can you provide more details about your environment? Where did you define app, hostname etc.?

This configuration works for app:

<configuration packages="pl.tkowalcz.tjahzi.log4j2">
    <Properties>
        <Property name="app">test</Property>
    </Properties>

 <appenders>
        <Loki name="Loki">   
            ...
            <Label name="app" value="${app}"/>
        </Loki>
 </appenders>
...

also setting hostname like this (before log4j is initialised): System.setProperty("hostname", "test"); works for ${sys:hostname}.

danieltaub commented 2 years ago

Very strange, when using the library with spring it's seems not to work for some reason

tkowalcz commented 2 years ago

If you provide me more details about your spring setup or some bootstrap class (with dependency information) I will investigate.

danieltaub commented 2 years ago

I'm using spring-boot 2.6.2 and the latest log4j2 version 2.17.1 Tested the lookups with other appenders such as the json appender and it's also seems not to work, so look like it's not related to the loki appender...