<?xml version="1.0" encoding="UTF-8"?>
<!--

    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.

-->
<!--
    Example log4j2 configuration that emits each log event as a single JSON
    object aligned with the OpenTelemetry log data model
    (https://opentelemetry.io/docs/specs/otel/logs/data-model/).

    Required runtime dependency (NOT included by default in BookKeeper):
        org.apache.logging.log4j:log4j-layout-template-json:${log4j.version}

    Add it to the classpath alongside log4j-core to enable JsonTemplateLayout.

    The event template lives in conf/otel-log-template.json. Customize it
    there (e.g. add resource attributes, drop fields, rename keys) without
    touching this file.

    Top-level fields follow the OTel data-model field naming (PascalCase):
      * "Timestamp"           = event time, ISO-8601 UTC.
      * "SeverityText"        = log4j level name ("INFO", "WARN", ...).
      * "Body"                = the log message string.
      * "TraceId" / "SpanId" / "TraceFlags"
                              = MDC keys conventionally set by the
                                OpenTelemetry log appender bridge; omitted
                                when absent.
      * "Attributes"          = OTel-semantic-conventions attributes for the
                                event:
                                  - thread.name / thread.id
                                  - code.namespace (= the logger name)
                                  - exception.type / .message / .stacktrace
                                Plus the full Log4j2 ContextData map (every
                                slog ".attr(...)" entry and any SLF4J MDC
                                key) merged in via the "mdc" resolver with
                                flatten=true.
-->
<Configuration status="warn" monitorInterval="10">
    <Properties>
        <Property name="bookkeeper.log.dir">.</Property>
        <Property name="bookkeeper.log.file">bookkeeper-server.json.log</Property>
        <Property name="bookkeeper.log.root.level">INFO</Property>
        <Property name="bookkeeper.log.root.appender">CONSOLE</Property>
        <Property name="otel.template">file:${sys:bookkeeper.conf.dir:-conf}/otel-log-template.json</Property>
    </Properties>
    <Appenders>
        <Console name="CONSOLE" target="SYSTEM_OUT">
            <JsonTemplateLayout eventTemplateUri="${otel.template}"/>
        </Console>
        <RollingFile name="ROLLINGFILE"
                     fileName="${sys:bookkeeper.log.dir}/${sys:bookkeeper.log.file}"
                     filePattern="${sys:bookkeeper.log.dir}/${sys:bookkeeper.log.file}%d{.yyyy-MM-dd}">
            <!--
                Each line is an independent JSON object (ndjson), directly
                consumable by log shippers (FluentBit, Vector, Fluentd,
                Promtail, otel-collector, ...).
            -->
            <JsonTemplateLayout eventTemplateUri="${otel.template}"/>
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true"/>
            </Policies>
            <DefaultRolloverStrategy max="100"/>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="${sys:bookkeeper.log.root.level}">
            <AppenderRef ref="${sys:bookkeeper.log.root.appender}"/>
        </Root>
    </Loggers>
</Configuration>
