Skip to content

Commit

Permalink
Add com.io7m.anethum.slf4j module for convenient logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
io7m committed Aug 12, 2023
1 parent e539f04 commit 9a23837
Show file tree
Hide file tree
Showing 8 changed files with 314 additions and 7 deletions.
7 changes: 6 additions & 1 deletion README-CHANGES.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<c:changelog project="com.io7m.anethum" xmlns:c="urn:com.io7m.changelog:4.0">
<c:releases>
<c:release date="2023-06-11T09:54:17+00:00" is-open="false" ticket-system="com.github.io7m.anethum" version="1.0.0">
<c:release date="2023-06-11T00:00:00+00:00" is-open="false" ticket-system="com.github.io7m.anethum" version="1.0.0">
<c:changes>
<c:change date="2023-06-11T00:00:00+00:00" summary="Initial major release."/>
</c:changes>
</c:release>
<c:release date="2023-08-12T21:39:48+00:00" is-open="true" ticket-system="com.github.io7m.anethum" version="1.1.0">
<c:changes>
<c:change date="2023-08-12T21:39:48+00:00" summary="Add com.io7m.anethum.slf4j module for convenient logging."/>
</c:changes>
</c:release>
</c:releases>
<c:ticket-systems>
<c:ticket-system default="true" id="com.github.io7m.anethum" url="https://www.github.com/io7m/anethum/issues/"/>
Expand Down
49 changes: 49 additions & 0 deletions com.io7m.anethum.slf4j/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>com.io7m.anethum</artifactId>
<groupId>com.io7m.anethum</groupId>
<version>1.1.0-SNAPSHOT</version>
</parent>
<artifactId>com.io7m.anethum.slf4j</artifactId>

<packaging>jar</packaging>
<name>com.io7m.anethum.slf4j</name>
<description>Generic parser API (SLF4J)</description>
<url>https://www.io7m.com/software/anethum</url>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>com.io7m.anethum.api</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>com.io7m.jlexing</groupId>
<artifactId>com.io7m.jlexing.core</artifactId>
</dependency>

<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.annotation.bundle</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.annotation.versioning</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright © 2023 Mark Raynsford <code@io7m.com> https://www.io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/


package com.io7m.anethum.slf4j;

import com.io7m.anethum.api.ParseStatus;
import org.slf4j.Logger;

/**
* Functions to log parse status messages.
*/

public final class ParseStatusLogging
{
private ParseStatusLogging()
{

}

/**
* Log a status message.
*
* @param logger The logger
* @param status The status
*/

public static void logMinimal(
final Logger logger,
final ParseStatus status)
{
switch (status.severity()) {
case PARSE_INFO -> {
logger.info(
"{}:{}: {}",
Integer.valueOf(status.lexical().line()),
Integer.valueOf(status.lexical().column()),
status.message()
);
}
case PARSE_WARNING -> {
logger.warn(
"{}:{}: {}",
Integer.valueOf(status.lexical().line()),
Integer.valueOf(status.lexical().column()),
status.message()
);
}
case PARSE_ERROR -> {
logger.error(
"{}:{}: {}",
Integer.valueOf(status.lexical().line()),
Integer.valueOf(status.lexical().column()),
status.message()
);
}
}
}

/**
* Log a status message.
*
* @param logger The logger
* @param status The status
*/

public static void logWithErrorCode(
final Logger logger,
final ParseStatus status)
{
switch (status.severity()) {
case PARSE_INFO -> {
logger.info(
"{}:{}: {}: {}",
Integer.valueOf(status.lexical().line()),
Integer.valueOf(status.lexical().column()),
status.errorCode(),
status.message()
);
}
case PARSE_WARNING -> {
logger.warn(
"{}:{}: {}: {}",
Integer.valueOf(status.lexical().line()),
Integer.valueOf(status.lexical().column()),
status.errorCode(),
status.message()
);
}
case PARSE_ERROR -> {
logger.error(
"{}:{}: {}: {}",
Integer.valueOf(status.lexical().line()),
Integer.valueOf(status.lexical().column()),
status.errorCode(),
status.message()
);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright © 2023 Mark Raynsford <code@io7m.com> http://io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

/**
* Generic parser API (SLF4J Functions)
*/

@Export
@Version("1.0.0")
package com.io7m.anethum.slf4j;

import org.osgi.annotation.bundle.Export;
import org.osgi.annotation.versioning.Version;
32 changes: 32 additions & 0 deletions com.io7m.anethum.slf4j/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright © 2023 Mark Raynsford <code@io7m.com> https://www.io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

/**
* Generic parser API (SLF4J Functions)
*/

module com.io7m.anethum.slf4j
{
requires static org.osgi.annotation.bundle;
requires static org.osgi.annotation.versioning;

requires com.io7m.anethum.api;

requires com.io7m.jlexing.core;
requires org.slf4j;

exports com.io7m.anethum.slf4j;
}
10 changes: 10 additions & 0 deletions com.io7m.anethum.tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@
<artifactId>com.io7m.anethum.api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>com.io7m.anethum.slf4j</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,31 @@

package com.io7m.anethum.tests;

import com.io7m.anethum.api.ParseSeverity;
import com.io7m.anethum.api.ParseStatus;
import com.io7m.anethum.slf4j.ParseStatusLogging;
import com.io7m.jlexing.core.LexicalPosition;
import net.jqwik.api.Arbitraries;
import net.jqwik.api.Arbitrary;
import net.jqwik.api.ForAll;
import net.jqwik.api.Property;
import net.jqwik.api.Provide;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;

public final class ParseStatusTest
{
private static final Logger LOG =
LoggerFactory.getLogger(ParseStatusTest.class);

@Provide
public Arbitrary<Throwable> throwables()
{
Expand All @@ -43,12 +52,12 @@ public Arbitrary<Throwable> throwables()
/**
* Mutable builders work as expected.
*
* @param errorCode The error code
* @param message1 A message
* @param message2 A message
* @param action An action
* @param errorCode The error code
* @param message1 A message
* @param message2 A message
* @param action An action
* @param attributes1 A set of attributes
* @param exception An exception
* @param exception An exception
*/

@Property
Expand Down Expand Up @@ -77,7 +86,10 @@ public void testBuilder(
Stream.concat(
attributes1.entrySet().stream(),
attributes2.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (s0, s1) -> s1));
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(s0, s1) -> s1));

final var error = builder.build();
assertEquals(message2, error.message());
Expand All @@ -86,4 +98,52 @@ public void testBuilder(
assertEquals(exception, error.exception().orElseThrow());
assertEquals(action, error.remediatingAction().orElseThrow());
}

/**
* Logging works as expected.
*
* @param severity The parse severity
* @param errorCode The error code
* @param message A message
*/

@Property
public void testLoggingMinimal(
final @ForAll ParseSeverity severity,
final @ForAll String errorCode,
final @ForAll String message,
final @ForAll int line)
{
ParseStatusLogging.logMinimal(
LOG,
ParseStatus.builder(errorCode, message)
.withSeverity(severity)
.withLexical(LexicalPosition.of(line, 0, Optional.empty()))
.build()
);
}

/**
* Logging works as expected.
*
* @param severity The parse severity
* @param errorCode The error code
* @param message A message
*/

@Property
public void testLoggingWithErrorCode(
final @ForAll ParseSeverity severity,
final @ForAll String errorCode,
final @ForAll String message,
final @ForAll int line)
{
ParseStatusLogging.logWithErrorCode(
LOG,
ParseStatus.builder(errorCode, message)
.withSeverity(severity)
.withLexical(LexicalPosition.of(line, 0, Optional.empty()))
.build()
);
}
}
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

<modules>
<module>com.io7m.anethum.api</module>
<module>com.io7m.anethum.slf4j</module>
<module>com.io7m.anethum.tests</module>
</modules>

Expand Down Expand Up @@ -116,6 +117,16 @@
<artifactId>com.io7m.xstructural.cmdline</artifactId>
<version>${com.io7m.xstructural.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.7</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.4.8</version>
</dependency>

<!-- Test suite. -->
<dependency>
Expand Down

0 comments on commit 9a23837

Please sign in to comment.