File tree 7 files changed +102
-0
lines changed
kotlin/com/baeldung/log4j
7 files changed +102
-0
lines changed Original file line number Diff line number Diff line change 21
21
<artifactId >kotlin-logging-jvm</artifactId >
22
22
<version >${kotlin-logging-jvm.version} </version >
23
23
</dependency >
24
+ <dependency >
25
+ <groupId >org.apache.logging.log4j</groupId >
26
+ <artifactId >log4j-core</artifactId >
27
+ <version >2.20.0</version >
28
+ </dependency >
29
+ <dependency >
30
+ <groupId >org.apache.logging.log4j</groupId >
31
+ <artifactId >log4j-api-kotlin</artifactId >
32
+ <version >1.5.0</version >
33
+ </dependency >
34
+
24
35
</dependencies >
25
36
26
37
<properties >
Original file line number Diff line number Diff line change
1
+ package com.baeldung.log4j
2
+
3
+ import org.apache.logging.log4j.kotlin.logger
4
+
5
+ class Enemy {
6
+ fun shoot () {
7
+ logger.warn { " Enemy shoots the player" }
8
+ }
9
+ }
Original file line number Diff line number Diff line change
1
+ import org.apache.logging.log4j.kotlin.logger
2
+
3
+ class Game {
4
+ companion object {
5
+ private val logger = logger()
6
+ }
7
+
8
+ fun startGame () {
9
+ logger.info { " Game is starting..." }
10
+ }
11
+
12
+ fun loadLevel (levelName : String ) {
13
+ logger.info { " Loading level: $levelName " }
14
+ try {
15
+ if (levelName.isBlank()) {
16
+ throw IllegalArgumentException (" Level name cannot be empty" )
17
+ }
18
+ logger.debug { " Level '$levelName ' loaded successfully." }
19
+ } catch (ex: Exception ) {
20
+ logger.error(ex) { " Error loading level: $levelName " }
21
+ }
22
+ }
23
+ }
24
+
25
+ // class Game { private val logger = logger() fun startGame() { logger.info { "Game is starting..." } } }
Original file line number Diff line number Diff line change
1
+ package com.baeldung.log4j
2
+
3
+ import org.apache.logging.log4j.kotlin.Logging
4
+
5
+ class GameUtil : Logging {
6
+ fun logEvent (event : String ) {
7
+ logger.info { " Game Event: $event " }
8
+ }
9
+ }
Original file line number Diff line number Diff line change
1
+ package com.baeldung.log4j
2
+
3
+ import Game
4
+
5
+ fun main () {
6
+ val game = Game ()
7
+ val player = Player (" Baeldung" )
8
+ val enemy = Enemy ()
9
+
10
+ game.startGame()
11
+ game.loadLevel(" Level 1" )
12
+
13
+ player.joinGame()
14
+ enemy.shoot()
15
+
16
+ player.takeDamage(100 )
17
+ GameUtil ().logEvent(" Player died" )
18
+
19
+ game.loadLevel(" " )
20
+ }
Original file line number Diff line number Diff line change
1
+ package com.baeldung.log4j
2
+
3
+ import org.apache.logging.log4j.kotlin.logger
4
+
5
+ class Player (val name : String ) {
6
+ private val logger = logger(" Player Action" )
7
+
8
+ fun joinGame () {
9
+ logger.info { " Player '$name ' has joined the game." }
10
+ }
11
+
12
+ fun takeDamage (amount : Int ) {
13
+ logger.warn { " Player '$name ' took $amount damage!" }
14
+ }
15
+ }
Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2
+ <Configuration status =" WARN" >
3
+ <Appenders >
4
+ <Console name =" ConsoleAppender" target =" SYSTEM_OUT" >
5
+ <PatternLayout pattern =" %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
6
+ </Console >
7
+ </Appenders >
8
+ <Loggers >
9
+ <Root level =" debug" >
10
+ <AppenderRef ref =" ConsoleAppender" />
11
+ </Root >
12
+ </Loggers >
13
+ </Configuration >
You can’t perform that action at this time.
0 commit comments