Skip to content

Commit 064a490

Browse files
authored
Merge pull request #6 from EndlessCodeGroup/feature/default-reporter
Fallback reporter
2 parents e17d840 + 29e13bc commit 064a490

4 files changed

Lines changed: 71 additions & 33 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## [Unreleased]
22

3+
### Fallback reporter implementation
4+
5+
Now you can return `null` from `createReporter`.
6+
In this case will be used `LoggerReporter` as a fallback.
7+
This reporter simply prints everything to the plugin's logger, just like you not use Inspector.
8+
39
### Changed
410

511
- Better interoperability with Java

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Also, it sends some sensitive data that can be disabled from sending:
1616
- List of plugins with versions
1717

1818
> NOTE: Inspector filters all tracked exceptions from console to not bother server owners.
19-
But all plugin-related logs will be saved to log file in the plugin folder.
19+
All plugin-related logs will be saved to log file in the plugin folder.
2020

2121
### Navigation
2222
- [samples](samples): Samples of usage Inspector
@@ -25,15 +25,15 @@ But all plugin-related logs will be saved to log file in the plugin folder.
2525

2626
## For server owners
2727
This is not a plugin and can't be installed with copying to `plugins` directory.
28-
But you can configure it.
29-
You can disable sending of information about server core and installed plugins in the `inspector.yml` that stored in directory of the each plugin that uses Inspector.
30-
Also you can configure it globally in `plugins/Inspector/config.yml`.
28+
29+
You can disable sending of information about server core and installed plugins in the `inspector.yml` that stored in a directory of each plugin that uses Inspector.
30+
Also, you can configure it globally in `plugins/Inspector/config.yml`.
3131

3232
### Config example
3333
```yaml
3434
Reporter:
3535
enabled: true
36-
# Here you can choose what you don't want to send
36+
# Here you can choose what you want to send
3737
data:
3838
core: true # Info about server core
3939
plugins: true # Plugins list
@@ -104,7 +104,7 @@ dependencies {
104104

105105
### Main plugin class modifications
106106

107-
First of all your current main plugin class should extend `PluginLifecycle` instead of `JavaPlugin`.
107+
First, your current main plugin class should extend `PluginLifecycle` instead of `JavaPlugin`.
108108
For example, this code:
109109
```java
110110
public class MyPlugin extends JavaPlugin {
@@ -122,19 +122,19 @@ public class MyPlugin extends PluginLifecycle {
122122
}
123123
```
124124

125-
If you doing in constructor any work that requires access to plugin's fields you can receive `UninitializedPropertyAccessException`.
126-
To avoid it override method init() and do the work within:
125+
If you're doing anything that requires access to plugin's methods in a constructor, you will get `UninitializedPropertyAccessException`.
126+
To avoid this problem, override method `init()` and do the work within:
127127
```java
128128
public class MyPlugin extends PluginLifecycle {
129129
@Override
130130
public void init() {
131-
// do some work
131+
// do some work, using plugin's methods
132132
}
133133
}
134134
```
135135

136-
When previous action done, you must create the new class extending `TrackedPlugin` that will be used as main plugin class and link it with the lifecycle.
137-
Also you must override method `createReporter()`. Created reporter will be used for reporting errors.
136+
Next, you must create the new class extending `TrackedPlugin` that will be used as main plugin class and link it with the lifecycle.
137+
Also, you must override method `createReporter()`. The created reporter will be used for reporting errors.
138138
Example:
139139
```java
140140
public class MyTrackedPlugin extends TrackedPlugin {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ru.endlesscode.inspector.report
2+
3+
import java.util.logging.Level
4+
import java.util.logging.Logger
5+
6+
/** Simply reports everything to the given [logger]. */
7+
public class LoggerReporter(
8+
private val logger: Logger,
9+
override val focus: ReporterFocus,
10+
) : Reporter {
11+
12+
override var enabled: Boolean = true
13+
14+
private val handlers = CompoundReportHandler()
15+
16+
override fun addHandler(handler: ReportHandler) {
17+
handlers.addHandler(handler)
18+
}
19+
20+
override fun report(message: String, exception: Exception, async: Boolean) {
21+
val exceptionData = ExceptionData(exception)
22+
handlers.beforeReport(message, exceptionData)
23+
logger.log(Level.WARNING, message, exception)
24+
handlers.onSuccess(message, exceptionData)
25+
}
26+
}

inspector-bukkit/src/main/kotlin/plugin/TrackedPlugin.kt

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ import ru.endlesscode.inspector.bukkit.report.BukkitEnvironment
1010
import ru.endlesscode.inspector.bukkit.report.BukkitUnwrapReporter
1111
import ru.endlesscode.inspector.bukkit.util.debug
1212
import ru.endlesscode.inspector.bukkit.util.fixDebugLogs
13-
import ru.endlesscode.inspector.report.ReportEnvironment
14-
import ru.endlesscode.inspector.report.ReportedException
15-
import ru.endlesscode.inspector.report.Reporter
16-
import ru.endlesscode.inspector.report.ReporterFocus
13+
import ru.endlesscode.inspector.report.*
1714
import ru.endlesscode.inspector.util.stackTraceText
1815
import java.io.File
1916
import java.io.InputStream
@@ -27,7 +24,6 @@ import java.util.logging.Formatter
2724
import java.util.logging.Level
2825
import java.util.logging.LogRecord
2926

30-
3127
/**
3228
* Class that takes plugin, and watches for all exceptions.
3329
*
@@ -51,7 +47,7 @@ public abstract class TrackedPlugin @JvmOverloads constructor(
5147
public val lifecycle: PluginLifecycle
5248

5349
init {
54-
initLogger()
50+
setupLoggerFile()
5551

5652
try {
5753
lifecycle = lifecycleClass.getDeclaredConstructor().newInstance()
@@ -64,24 +60,11 @@ public abstract class TrackedPlugin @JvmOverloads constructor(
6460
lifecycle.holder = this
6561
track { lifecycle.init() }
6662

67-
reporter = BukkitUnwrapReporter(createReporter())
63+
reporter = createReporter()?.let(::configureReporter) ?: LoggerReporter(logger, focus = this)
6864
reporter.enabled = environment.isInspectorEnabled
69-
reporter.addHandler(
70-
beforeReport = { message, data ->
71-
logger.debug("$TAG $message", data.exception)
72-
},
73-
onSuccess = { message, _ ->
74-
logger.warning("$TAG $message")
75-
logger.warning("$TAG Error was reported to author!")
76-
},
77-
onError = {
78-
logger.warning("$TAG Error on report: ${it.localizedMessage}")
79-
logger.debug(TAG, it)
80-
}
81-
)
8265
}
8366

84-
private fun initLogger() {
67+
private fun setupLoggerFile() {
8568
val logsDir = Files.createDirectories(Paths.get("${dataFolder.path}/logs"))
8669
val fileHandler = FileHandler("$logsDir/latest.log", true).apply {
8770
level = Level.ALL
@@ -98,7 +81,30 @@ public abstract class TrackedPlugin @JvmOverloads constructor(
9881
logger.fixDebugLogs()
9982
}
10083

101-
protected abstract fun createReporter(): Reporter
84+
private fun configureReporter(reporter: Reporter): BukkitUnwrapReporter {
85+
return BukkitUnwrapReporter(reporter).apply {
86+
addHandler(
87+
beforeReport = { message, data ->
88+
logger.debug("$TAG $message", data.exception)
89+
},
90+
onSuccess = { message, _ ->
91+
logger.warning("$TAG $message")
92+
logger.warning("$TAG Error was reported to author!")
93+
},
94+
onError = {
95+
logger.warning("$TAG Error on report: ${it.localizedMessage}")
96+
logger.debug(TAG, it)
97+
}
98+
)
99+
}
100+
}
101+
102+
/**
103+
* Creates reporter.
104+
*
105+
* Return `null` if you want fallback to [LoggerReporter].
106+
*/
107+
protected abstract fun createReporter(): Reporter?
102108

103109
final override fun getConfig(): FileConfiguration {
104110
return super.getConfig()

0 commit comments

Comments
 (0)