Skip to content

Commit 6c1ece2

Browse files
committed
Reorganize Logger.kt file
1 parent a2b615c commit 6c1ece2

File tree

1 file changed

+86
-86
lines changed
  • src/commonMain/kotlin/dev/hermannm/devlog

1 file changed

+86
-86
lines changed

src/commonMain/kotlin/dev/hermannm/devlog/Logger.kt

Lines changed: 86 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -5,92 +5,6 @@ package dev.hermannm.devlog
55

66
import kotlin.reflect.KClass
77

8-
/**
9-
* Returns a [Logger], with its name inferred from the class in which it's called (or file, if
10-
* defined at the top level).
11-
*
12-
* The logger name is included in the log output, and can be used to enable/disable log levels for
13-
* loggers based on their package names, or query for logs from a specific class.
14-
*
15-
* ### Example
16-
*
17-
* ```
18-
* // In file Example.kt
19-
* package com.example
20-
*
21-
* import dev.hermannm.devlog.getLogger
22-
*
23-
* // Gets the name "com.example.Example"
24-
* private val log = getLogger()
25-
*
26-
* fun example() {
27-
* log.info { "Example message" }
28-
* }
29-
* ```
30-
*
31-
* ### Implementation
32-
*
33-
* In the JVM implementation, this calls `MethodHandles.lookup().lookupClass()`, which returns the
34-
* calling class. Since this function is inline, that will actually return the class that called
35-
* `getLogger`, so we can use it to get the name of the caller. When called at file scope, the
36-
* calling class will be the synthetic `Kt` class that Kotlin generates for the file, so we can use
37-
* the file name in that case.
38-
*
39-
* This is the pattern that
40-
* [the SLF4J docs recommends](https://www.slf4j.org/faq.html#declaration_pattern) for instantiating
41-
* loggers in a generic manner.
42-
*/
43-
public expect inline fun getLogger(): Logger
44-
45-
/**
46-
* Returns a [Logger] with the name of the given class.
47-
*
48-
* The logger name is included in the log output, and can be used to enable/disable log levels for
49-
* loggers based on their package names, or query for logs from a specific class.
50-
*
51-
* In most cases, you should prefer the zero-argument `getLogger()` overload, to automatically get
52-
* the name of the containing class (or file). But if you want more control over which class to use
53-
* for the logger name, you can use this overload.
54-
*
55-
* ### Example
56-
*
57-
* ```
58-
* package com.example
59-
*
60-
* import dev.hermannm.devlog.getLogger
61-
*
62-
* class Example {
63-
* companion object {
64-
* // Gets the name "com.example.Example"
65-
* private val log = getLogger(Example::class)
66-
* }
67-
*
68-
* fun example() {
69-
* log.info { "Example message" }
70-
* }
71-
* }
72-
* ```
73-
*/
74-
public expect fun getLogger(forClass: KClass<*>): Logger
75-
76-
/**
77-
* Returns a [Logger] with the given name.
78-
*
79-
* The logger name is included in the log output, and can be used to enable/disable log levels for
80-
* loggers based on their package names, or query for logs from a specific class. Because of this,
81-
* the name given here should follow fully qualified class name format, like `com.example.Example`.
82-
*
83-
* To set the name automatically from the containing class/file, you can use the zero-argument
84-
* `getLogger()` overload instead.
85-
*
86-
* ### Example
87-
*
88-
* ```
89-
* private val log = getLogger(name = "com.example.Example")
90-
* ```
91-
*/
92-
public expect fun getLogger(name: String): Logger
93-
948
/**
959
* A logger provides methods for logging at various log levels ([info], [warn], [error], [debug] and
9610
* [trace]). It has a logger name, typically the same as the class that the logger is attached to
@@ -537,6 +451,92 @@ internal constructor(
537451
}
538452
}
539453

454+
/**
455+
* Returns a [Logger], with its name inferred from the class in which it's called (or file, if
456+
* defined at the top level).
457+
*
458+
* The logger name is included in the log output, and can be used to enable/disable log levels for
459+
* loggers based on their package names, or query for logs from a specific class.
460+
*
461+
* ### Example
462+
*
463+
* ```
464+
* // In file Example.kt
465+
* package com.example
466+
*
467+
* import dev.hermannm.devlog.getLogger
468+
*
469+
* // Gets the name "com.example.Example"
470+
* private val log = getLogger()
471+
*
472+
* fun example() {
473+
* log.info { "Example message" }
474+
* }
475+
* ```
476+
*
477+
* ### Implementation
478+
*
479+
* In the JVM implementation, this calls `MethodHandles.lookup().lookupClass()`, which returns the
480+
* calling class. Since this function is inline, that will actually return the class that called
481+
* `getLogger`, so we can use it to get the name of the caller. When called at file scope, the
482+
* calling class will be the synthetic `Kt` class that Kotlin generates for the file, so we can use
483+
* the file name in that case.
484+
*
485+
* This is the pattern that
486+
* [the SLF4J docs recommends](https://www.slf4j.org/faq.html#declaration_pattern) for instantiating
487+
* loggers in a generic manner.
488+
*/
489+
public expect inline fun getLogger(): Logger
490+
491+
/**
492+
* Returns a [Logger] with the name of the given class.
493+
*
494+
* The logger name is included in the log output, and can be used to enable/disable log levels for
495+
* loggers based on their package names, or query for logs from a specific class.
496+
*
497+
* In most cases, you should prefer the zero-argument `getLogger()` overload, to automatically get
498+
* the name of the containing class (or file). But if you want more control over which class to use
499+
* for the logger name, you can use this overload.
500+
*
501+
* ### Example
502+
*
503+
* ```
504+
* package com.example
505+
*
506+
* import dev.hermannm.devlog.getLogger
507+
*
508+
* class Example {
509+
* companion object {
510+
* // Gets the name "com.example.Example"
511+
* private val log = getLogger(Example::class)
512+
* }
513+
*
514+
* fun example() {
515+
* log.info { "Example message" }
516+
* }
517+
* }
518+
* ```
519+
*/
520+
public expect fun getLogger(forClass: KClass<*>): Logger
521+
522+
/**
523+
* Returns a [Logger] with the given name.
524+
*
525+
* The logger name is included in the log output, and can be used to enable/disable log levels for
526+
* loggers based on their package names, or query for logs from a specific class. Because of this,
527+
* the name given here should follow fully qualified class name format, like `com.example.Example`.
528+
*
529+
* To set the name automatically from the containing class/file, you can use the zero-argument
530+
* `getLogger()` overload instead.
531+
*
532+
* ### Example
533+
*
534+
* ```
535+
* private val log = getLogger(name = "com.example.Example")
536+
* ```
537+
*/
538+
public expect fun getLogger(name: String): Logger
539+
540540
/**
541541
* Platform-neutral interface for the underlying logger implementation used by [Logger].
542542
*

0 commit comments

Comments
 (0)