Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code cleanups an minor changes #39

Merged
merged 6 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ the PATCH component is omitted when its value is `0`.
#### Changed

- Added `suppressed` throwables to the logged exceptions; ([#29](https://github.com/hkupty/penna/pull/29))
- Minilogger now logs in json; ([#39](https://github.com/hkupty/penna/pull/39))

#### Removed

Expand All @@ -32,6 +33,10 @@ the PATCH component is omitted when its value is `0`.

### `penna-yaml-config`

#### Changed

- Added pmd and static analysis checks to `penna-yaml-config`; ([#39](https://github.com/hkupty/penna/pull/39))

#### Fixed

- Fix reading yaml config from `penna.yaml` inside the jar; ([#38](https://github.com/hkupty/penna/pull/38))
4 changes: 4 additions & 0 deletions penna-core/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@

// The only user-facing namespace should be config.
exports penna.core.config;

// Penna subprojects also have access to minilog
// when/if breaking apart from slf4j, expose the full logger
exports penna.core.minilog to penna.config.yaml;
}
14 changes: 8 additions & 6 deletions penna-core/src/main/java/penna/core/minilog/MiniLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,27 @@
public final class MiniLogger {
private MiniLogger() {}
public static void error(String message) {
System.err.print("[:Penna.MiniLogger/Error \"");
System.err.print("{\"logger\":\"penna.core.MiniLogger\",\"level\":\"ERROR\",\"message\":\"");
System.err.print(message);
System.err.println("\"]");
System.err.println("\"}");
}

public static void error(String message, Throwable throwable) {
error(message);
System.err.print("[:Penna.MiniLogger/Exception ");
System.err.print("{\"logger\":\"penna.core.MiniLogger\",\"level\":\"ERROR\",\"message\":\"");
System.err.println(throwable.getMessage());
System.err.println("\", \"throwable\":\"");
var stack = throwable.getStackTrace();
for(int i = 0; i < stack.length; i++) {
System.err.println(stack[i].toString());
System.err.println("\n");
}
System.err.print("]");
System.err.println("\"}");
}

public static void debug(String message) {
System.err.print("[:Penna/MiniLogger/Debug \"");
System.err.print("{\"logger\":\"penna.core.MiniLogger\",\"level\":\"ERROR\",\"message\":\"");
System.err.print(message);
System.err.println("\"]");
System.err.println("\"}");
}
}
6 changes: 6 additions & 0 deletions penna-yaml-config/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
plugins {
id 'penna.base-java'
id 'penna.publishing'
id 'pmd'
}

group 'com.hkupty.penna'
version '0.5.1'

pmd {
sourceSets = [sourceSets.main]
ruleSets("category/java/performance.xml", "category/java/bestpractices.xml", "category/java/errorprone.xml")
}

repositories {
mavenCentral()
Expand All @@ -14,6 +19,7 @@ repositories {
dependencies {
implementation 'org.slf4j:slf4j-api:2.0.6'
implementation project(":penna-api")
implementation project(":penna-core")
compileOnly 'com.fasterxml.jackson.core:jackson-core:2.14.2'
compileOnly 'com.fasterxml.jackson.core:jackson-databind:2.14.2'
compileOnly 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.14.2'
Expand Down
1 change: 1 addition & 0 deletions penna-yaml-config/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

requires static com.fasterxml.jackson.databind;
requires static com.fasterxml.jackson.dataformat.yaml;
requires penna.core;

provides penna.api.config.ConfigManager with YamlConfigManager;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@
import penna.api.config.Configurable;
import penna.config.yaml.impl.JacksonConfigManager;

import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;


public class YamlConfigManager implements ConfigManager {
ConfigManager impl;
transient ConfigManager impl;

private static ConfigManager tryJackson(URL configFile) throws ClassNotFoundException {
Class.forName("com.fasterxml.jackson.dataformat.yaml.YAMLMapper");
Expand All @@ -22,9 +18,9 @@ private static ConfigManager tryJackson(URL configFile) throws ClassNotFoundExce

public YamlConfigManager(){
try {
var url = Objects.requireNonNull(getClass().getClassLoader().getResource("penna.yaml"));
var url = Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("penna.yaml"));
impl = tryJackson(url);
} catch (NullPointerException | ClassNotFoundException ignored) {
} catch (ClassNotFoundException ignored) {
impl = new ConfigManager() {
private Configurable configurable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,33 @@
import penna.api.models.LogField;
import org.slf4j.event.Level;
import penna.config.yaml.models.Node;
import penna.core.minilog.MiniLogger;

import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.Locale;
import java.util.Objects;

public class JacksonConfigManager implements ConfigManager {
private static final LogField[] reference = new LogField[]{};
private final ObjectMapper mapper;
private final URL file;
Configurable configurable;
Node.RootNode config;
private transient final ObjectMapper mapper;
private transient final URL file;
transient Configurable configurable;
transient Node.RootNode config;

public JacksonConfigManager(URL file) {
this.mapper = new YAMLMapper();
this.file = file;
}

@SuppressWarnings("PMD.DataflowAnomalyAnalysis")
private ConfigurationChange getUpdateFn(Node configNode) {
var hasFields = (configNode.fields() != null) && (!configNode.fields().isEmpty());
var hasLevel = configNode.level() != null;

return ((Config base) -> base
.replaceLevel(hasLevel ? Level.valueOf(configNode.level().toUpperCase()) : base.level())
.replaceLevel(hasLevel ? Level.valueOf(configNode.level().toUpperCase(Locale.ENGLISH)) : base.level())
.replaceFields(hasFields ? configNode.fields().stream().map(LogField::fromFieldName).filter(Objects::nonNull).toArray(size -> Arrays.copyOf(reference, size)) : base.fields()));
}

Expand Down Expand Up @@ -72,7 +75,7 @@ public void configure() {
read();
configurable.configure(configItemsFromYaml());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
MiniLogger.error("Unable to read configuration", ioe);
}
}

Expand All @@ -82,18 +85,3 @@ public void updateConfigs(ConfigItem... configItems) {
}
}

/* The yaml should look something like this:

- penna:
level: info // optional
fields:
- thread
- logger
- message
- mdc
loggers:
penna:
level: debug
penna.core.logger:
level: trace
*/