Skip to content

Commit

Permalink
ported markers rewrite to 3.x (#2584)
Browse files Browse the repository at this point in the history
  • Loading branch information
grobmeier committed May 22, 2024
1 parent 4591911 commit 891c08e
Show file tree
Hide file tree
Showing 9 changed files with 371 additions and 82 deletions.
1 change: 1 addition & 0 deletions src/site/antora/antora.tmpl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ start_page: index.adoc
asciidoc:
attributes:
# Commons
antora-examples-url: "https://raw.githubusercontent.com/apache/logging-log4j2/2.x/src/site/antora/modules/ROOT/examples"
project-github-url: "${scm.url}"
project-name: "Log4j"
project-id: "log4j"
Expand Down
1 change: 1 addition & 0 deletions src/site/antora/antora.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ start_page: index.adoc
asciidoc:
attributes:
# Commons
antora-examples-url: "https://raw.githubusercontent.com/apache/logging-log4j2/2.x/src/site/antora/modules/ROOT/examples"
project-github-url: "https:/apache/logging-log4j2"
project-name: "Log4j"
project-id: "log4j"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.
*/
package example;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;

public final class MarkerExample {

private static final Logger logger = LogManager.getLogger("example.MarkerExample");
// tag::create-marker[]
private static final Marker SQL_MARKER = MarkerManager.getMarker("SQL");
// end::create-marker[]
// tag::create-marker-parent[]
private static final Marker QUERY_MARKER =
MarkerManager.getMarker("SQL_QUERY").addParents(SQL_MARKER);
private static final Marker UPDATE_MARKER =
MarkerManager.getMarker("UPDATE").addParents(SQL_MARKER);
// end::create-marker-parent[]

public static void main(final String[] args) {
doQuery("my_table");
doQueryParent("my_table");
doUpdate("my_table", "column", "value");
}

public static void doQuery(String table) {
// Do business logic here
// tag::use-marker[]
logger.debug(SQL_MARKER, "SELECT * FROM {}", table);
// end::use-marker[]
}

public static void doQueryParent(String table) {
// Do business logic here
// tag::use-marker-parent[]
logger.debug(QUERY_MARKER, "SELECT * FROM {}", table);
// end::use-marker-parent[]
}

public static void doUpdate(String table, String column, String value) {
// Do business logic here
// tag::use-marker-parent[]
logger.debug(UPDATE_MARKER, "UPDATE {} SET {} = {}", table, column, value);
// end::use-marker-parent[]
}
}
31 changes: 31 additions & 0 deletions src/site/antora/modules/ROOT/examples/manual/markers/log4j2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"Configuration": {
"Appenders": {
"Console": {
"name": "SQL_LOG",
"PatternLayout": {
"pattern": "%d{HH:mm:ss.SSS} (%marker) %m%n"
}
}
},
"Loggers": {
"Root": {
"level": "INFO"
},
// tag::logger[]
"Logger": {
"name": "example",
"level": "ALL", // <1>
"AppenderRef": {
"ref": "SQL_LOG",
"MarkerFilter": { // <2>
"marker": "SQL",
"onMatch": "ACCEPT",
"onMismatch": "DENY"
}
}
}
// end::logger[]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#
# 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.
#
appender.0.type = Console
appender.0.name = SQL_LOG
appender.0.layout.type = PatternLayout
appender.0.layout.pattern = %d{HH:mm:ss.SSS} (%marker) %m%n

rootLogger.level = INFO

# tag::logger[]
logger.0.name = example
# <1>
logger.0.level = ALL
logger.0.appenderRef.0.ref = SQL_LOG
# <2>
logger.0.appenderRef.0.filter.type = MarkerFilter
logger.0.appenderRef.0.filter.marker = SQL
logger.0.appenderRef.0.filter.onMatch = ACCEPT
logger.0.appenderRef.0.filter.onMismatch = DENY
# end::logger[]
43 changes: 43 additions & 0 deletions src/site/antora/modules/ROOT/examples/manual/markers/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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.
-->
<Configuration xmlns="https://logging.apache.org/xml/ns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
https://logging.apache.org/xml/ns
https://logging.apache.org/xml/ns/log4j-config-2.xsd">

<appenders>
<Console name="SQL_LOG">
<PatternLayout pattern="%d{HH:mm:ss.SSS} (%marker) %m%n"/>
</Console>
</appenders>

<loggers>
<root level="INFO"/>
<!-- tag::logger[] -->
<logger name="example" level="ALL"><!--1-->
<AppenderRef ref="SQL_LOG">
<MarkerFilter marker="SQL"
onMatch="ACCEPT"
onMismatch="DENY"/><!--2-->
</AppenderRef>
</logger>
<!-- end::logger[] -->
</loggers>

</Configuration>
36 changes: 36 additions & 0 deletions src/site/antora/modules/ROOT/examples/manual/markers/log4j2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#
# 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.
#
Configuration:
Appenders:
Console:
name: "SQL_LOG"
PatternLayout:
pattern: "%d{HH:mm:ss.SSS} (%marker) %m%n"
Loggers:
Root:
level: "INFO"
# tag::logger[]
Logger:
name: "example"
level: "ALL" # <1>
AppenderRef:
ref: "SQL_LOG"
MarkerFilter: # <2>
marker: "SQL"
onMatch: "ACCEPT"
onMismatch: DENY
# end::logger[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#
# 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.
#
# tag::use-marker[]
10:42:30.982 (SQL) SELECT * FROM my_table
# end::use-marker[]
# tag::use-marker-parent[]
10:42:30.982 (SQL_QUERY[ SQL ]) SELECT * FROM my_table
10:42:30.982 (SQL_UPDATE[ SQL ]) UPDATE my_table SET column = value
# end::use-marker-parent[]
Loading

0 comments on commit 891c08e

Please sign in to comment.