Comments (4)
The Log4j configuration is a tree structure organized as "Nodes" where a Node is nothing but a container of attributes and child Nodes. All the configuration dialects Log4j supports are converted into a Node tree. This works great for Yaml, JSON, and XML. Unfortunately, by their nature properties are not hierarchical. This means using properties is somewhat fitting a round peg in a square hole. For years Log4j didn't support properties due to this but we finally added it due to user's requests. We have made slight "improvements" over time at user's requests but in the end I really don't think it makes it any simpler.
As an example of how Log4j configuration works, a more complete form of your example above
appender.console.name = STDOUT
appender.console.type = Console
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %m%n
appender.file.name = File
appender.file.type = File
appender.file.fileName = ${filename}
appender.file.bufferedIO = false
appender.file.layout.type = PatternLayout
appender.file.layout.pattern = %d %p %C{1.} [%t] %m%n
would correspond to:
XML
<Console name="STDOUT">
<PatternLayout pattern="%m%n"/>
</Console>
<File name="File" fileName="${filename}" bufferedIO="false">
<PatternLayout>
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</PatternLayout>
</File>
JSON
"appenders" : {
"Console" : {
"name" : "STDOUT",
"PatternLayout" : {
"Pattern": "%m%n"
}
},
"File" : {
"name": "File",
"fileName": "${filename}",
"bufferedIO": false,
"PatternLayout" : {
"Pattern": "%d %p %C{1.} [%t] %m%n"
}
}
}
YAML
appenders:
Console:
name: STDOUT
PatternLayout:
Pattern: "%m%n"
File:
name: File
fileName: ${filename}
bufferedIO: false
PatternLayout:
Pattern: "%d %p %C{1.} [%t] %m%n"
In the properties format the first identifier lets Log4j know that this is a child Appender under the Appenders node. The "console" and "file" are tokens that indicate all the properties with that token are grouped together. Finally, the final tokens represent the attributes of the appender with the exception of the "type", which indicates that it is a "Console" appender. Note that the Console appender also has a second token used for grouping; "layout". The type indicates it is a PattermLayout and the pattern is the attribute for that.
I should note that the way Log4j 1 handled configuration was completely different. Properties were the original and only format. XML was bolted on after the fact and was basically a different configuration system from using properties. In addition, Log4j 1.x didn't support the "type". It required every component have a class attribute with the fully qualified class name instead. So where you see a type in the properties configuration you would have replaced with a class token. Although in many examples the "type" and the "name" might have the same value the name used is entirely up to the user. So if your configuration has multiple File Appenders both would have a type of "File" but each would have a unique name.
I, for one, highly encourage users to use a different one of the other formats.
I should also add that anyone can write a new configuration factory. You can even replace the PropertiesConfigurationFactory with one of your own if you desire.
from logging-log4j2.
The identifiers in the properties format are almost useless. One of the last attempts to make the properties format more digestible (#733 and related issues) proposed to use them as default appender names, but the feature never made it out to the main code.
Such a usage would be in line with what Log4j 1.x did, but is not coherent with other formats: most formats use the identifiers/keys/element names to indicate the plugin type. You can see it in Ralph's YAML example:
appenders:
Console:
name: STDOUT
PatternLayout:
Pattern: "%m%n"
File:
name: FILE
fileName: ${filename}
bufferedIO: false
PatternLayout:
Pattern: "%d %p %C{1.} [%t] %m%n"
The keys Console
, File
and PatternLayout
associated to YAML objects are used as default values for the missing type
properties.
We could probably implement something similar in the properties format:
appender.Console.name = STDOUT
appender.Console.PatternLayout.pattern = "%m%n"
appender.File.name = FILE
appender.File.fileName = ${filename}
appender.File.bufferedIO = false
appender.File.PatternLayout.pattern = %d %p %C{1.} [%t] %m%n
However there isn't much to be done if multiple appenders of the same type are required. In the YAML format we currently support the following shorthand:
Appenders:
File:
- name: FILE1
fileName: file1.log
- name: FILE2
fileName: file2.log
(i.e. for YAML arrays the default type is the key associated to the array). I don't think anything similar would be possible for the properties format.
from logging-log4j2.
Dear @rgoers,
Thank you very much for your quick answer! You are really kind!
Best Regards: Bela
from logging-log4j2.
Dear @ppkarwasz,
Thank you for the details!
Regards: Bela
from logging-log4j2.
Related Issues (20)
- Revamp the `Configuration` page HOT 2
- Revamp the `Lookups` page
- Revamp the `Layouts` page
- Revamp the `Appenders` page HOT 1
- Revamp the `Filters` page
- Revamp the `Extending` page
- Revamp the `Plugins` page
- Create a `Troubleshooting` page HOT 1
- Revamp the `Performance` page
- Revamp the `Asynchronous logging` page
- Revamp the `API` page
- Uniform handling of location and URIs HOT 1
- [MS14] Documentation revamp 1/5 HOT 1
- [MS15] Documentation revamp 2/5
- [MS16] Documentation revamp 3/5 HOT 1
- [MS17] Documentation revamp 4/5
- [MS18] Documentation revamp 5/5
- Revamp the `Web Applications` page
- Implement uniform password "encryption/decryption" mechanism HOT 4
- When logging from multiple threads, an appender can be broken until app restart if it rolls over HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from logging-log4j2.