You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -45,77 +45,163 @@ You need to have the `org.apache.logging.log4j:log4j-api-kotlin` dependency in y
45
45
46
46
Java module name and OSGi `Bundle-SymbolicName` are set to `org.apache.logging.log4j.api.kotlin`.
47
47
48
-
[#usage]
49
-
== Usage
48
+
[#create-loggers]
49
+
== Creating loggers
50
50
51
-
You can start using the wrapper by extending from the provided `Logging` interface:
51
+
A `Logger` is the primary interface that users interact with Log4j Kotlin.
52
+
You can create ``Logger``s particularly in two ways:
53
+
54
+
* <<class-loggers,Associate them with the class>> (*Recommended!*)
55
+
* <<instance-loggers,Associate them with the instance>>
56
+
57
+
[#class-loggers]
58
+
=== [[usage]] Creating class loggers
59
+
60
+
For most applications, we recommend you to create *a single logger instance per class definition* – not <<instance-loggers,per class instance>>!
61
+
This not only avoids creating an extra logger field for each instance, its access pattern transparently communicates the implementation: the `Logger` is statically bound to the class definition.
62
+
You can create class loggers in one of following ways:
63
+
64
+
[#create-companion-logger]
65
+
==== Creating a logger in the companion object
66
+
67
+
This is the traditional approach to create class loggers.
68
+
It also happens to be the most efficient one, since the logger lookup is performed once and its result is stored in the companion object shared by all instances of the class.
52
69
53
70
[source,kotlin]
54
71
----
55
-
import org.apache.logging.log4j.kotlin.Logging
72
+
import org.apache.logging.log4j.kotlin.logger
73
+
74
+
class DbTableService {
75
+
76
+
companion object {
56
77
57
-
class MyClass: BaseClass, Logging {
78
+
private val LOGGER = logger() // <1>
58
79
59
-
fun doStuff() {
60
-
logger.info("Doing stuff")
61
80
}
62
81
63
-
fun doStuffWithUser(user: User) {
64
-
logger.info { "Doing stuff with ${user.name}." }
82
+
fun truncateTable(tableName: String) {
83
+
LOGGER.warn { "truncating table `${tableName}`" }
84
+
db.truncate(tableName)
65
85
}
66
86
67
87
}
68
88
----
89
+
<1> Create a `Logger` associated with the static class definition that all instances of the class share
69
90
70
-
The `Logging` interface can also be mixed into `object` declarations, including companions.
71
-
This is generally preferable over the previous approach as there is a single logger created for every instance of the class.
91
+
[#extend-companion]
92
+
==== Extending companion object from `Logging`
93
+
94
+
`Logging` interface contains a `logger` getter that you can use by extending the companion object from the `Logging` class:
72
95
73
96
[source,kotlin]
74
97
----
75
98
import org.apache.logging.log4j.kotlin.Logging
76
99
77
-
class MyClass: BaseClass {
100
+
class DbTableService {
78
101
79
-
companion object: Logging
102
+
companion object: Logging // <1>
80
103
81
-
// ...
104
+
fun truncateTable(tableName: String) {
105
+
logger.warn { "truncating table `${tableName}`" }
106
+
db.truncate(tableName)
107
+
}
82
108
83
109
}
84
110
----
111
+
<1> Extending the companion object from `Logging` effectively creates a single `Logger` instance
112
+
. Assigned to the `logger` field
113
+
. Associated with the static class definition that all instances of the class share
114
+
115
+
[NOTE]
116
+
====
117
+
This getter-based approach incurs an extra overhead (compared to <<create-companion-logger>>) due to the logger lookup involved at runtime.
118
+
====
85
119
86
-
Alternatively, a more traditional style can be used to instantiate a logger instance:
120
+
[#instance-loggers]
121
+
=== Creating instance loggers
122
+
123
+
Even though we recommend you to <<class-loggers,create class loggers>>, there might be occasions (most notably while {logging-services-url}/log4j/2.x/manual/webapp.html#log-separation[sharing classes in Jakarta EE environments]) necessitating loggers associated with each instance.
124
+
You can achieve this as follows:
125
+
126
+
[#create-instance-logger]
127
+
==== Creating a logger in the class
128
+
129
+
This is the traditional approach to create instance loggers.
130
+
It also happens to be the most efficient one, since the logger lookup is performed once and its result is stored in the instance field.
87
131
88
132
[source,kotlin]
89
133
----
90
134
import org.apache.logging.log4j.kotlin.logger
91
135
92
-
class MyClass: BaseClass {
136
+
class DbTableService {
93
137
94
-
val logger = logger()
138
+
private val logger = logger() // <1>
95
139
96
-
// ...
140
+
fun truncateTable(tableName: String) {
141
+
logger.warn { "truncating table `${tableName}`" }
142
+
db.truncate(tableName)
143
+
}
97
144
98
145
}
99
146
----
147
+
<1> Create a `Logger` associated with the class instance
100
148
101
-
The function `logger()` is an extension function on the `Any` type (or more specifically, any type `T` that extends `Any`).
149
+
[#extend-instance]
150
+
==== Extending the class from `Logging`
102
151
103
-
Beginning in version 1.3.0, an extension property is also available on classes:
152
+
`Logging` interface contains a `logger` getter that you can use by extending the class from `Logging`:
153
+
154
+
[source,kotlin]
155
+
----
156
+
import org.apache.logging.log4j.kotlin.Logging
157
+
158
+
class DbTableService: Logging { // <1>
159
+
160
+
fun truncateTable(tableName: String) {
161
+
logger.warn { "truncating table `${tableName}`" }
162
+
db.truncate(tableName)
163
+
}
164
+
165
+
}
166
+
----
167
+
<1> Extending the class from `Logging` effectively creates a single `Logger` instance
168
+
. Assigned to the `logger` field
169
+
. Exclusively associated with the class instance (i.e., not shared among instances!)
170
+
171
+
[NOTE]
172
+
====
173
+
This getter-based approach incurs an extra overhead (compared to <<create-instance-logger>>) due to the logger lookup involved at runtime.
174
+
====
175
+
176
+
[#logger-extension]
177
+
==== Using `logger` extension property
178
+
179
+
You can use the `logger` extension property to dynamically inject a logger at the spot:
0 commit comments