Use slf4j
All checks were successful
/ build (push) Successful in 31s

Signed-off-by: Minecon724 <git@m724.eu>
This commit is contained in:
Minecon724 2025-02-25 09:42:30 +01:00
parent 159519c94d
commit 0aadf69a42
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
6 changed files with 52 additions and 23 deletions

View file

@ -10,6 +10,8 @@ import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.io.file.PathUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.RepositoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
@ -27,6 +29,8 @@ import java.util.stream.Collectors;
* source for the blog's content and configuration.
*/
public class BlogBuilder {
private static final Logger LOGGER = LoggerFactory.getLogger(BlogBuilder.class);
private final Git git;
private final Path workingDirectory;
@ -121,7 +125,7 @@ public class BlogBuilder {
* @throws IOException if an I/O error occurs
*/
public void build() throws IOException {
System.out.println("Loading site...");
LOGGER.debug("Loading site...");
if (site == null)
this.site = Site.fromConfig(workingDirectory.resolve("site.yml"));
@ -131,21 +135,21 @@ public class BlogBuilder {
if (template == null)
this.template = new TemplateRenderer(templateDirectory);
System.out.println("Copying assets...");
LOGGER.debug("Copying assets...");
copyTree(workingDirectory.resolve("assets"), outputDirectory.resolve("assets"));
copyTree(templateDirectory.resolve("static"), outputDirectory.resolve("static"));
System.out.println("Rendering posts...");
LOGGER.debug("Rendering posts...");
var posts = renderPosts();
System.out.println("Rendering meta...");
LOGGER.debug("Rendering meta...");
posts.sort(Comparator.comparing(Post::createdAt).reversed());
Files.writeString(outputDirectory.resolve("index.html"), template.renderIndex(site, posts));
Files.writeString(outputDirectory.resolve("posts.rss"), Feed.generateRss(site, posts));
if (!renderOptions.compress().isEmpty()) {
System.out.println("Compressing...");
LOGGER.debug("Compressing...");
compressOutput();
}
}
@ -162,7 +166,7 @@ public class BlogBuilder {
continue; // directory is created below
if (!path.toString().endsWith(".html")) {
System.out.println("Post " + path.getFileName() + ": unsupported file type");
LOGGER.warn("Post {}: unsupported file type", path.getFileName());
continue;
}
@ -170,7 +174,7 @@ public class BlogBuilder {
var post = Post.fromFile(git, path);
if (post.draft() && !renderDrafts) {
System.out.println("Post " + path.getFileName() + ": draft, ignoring");
LOGGER.info("Post {}: draft, ignoring", path.getFileName());
continue;
}
@ -204,7 +208,7 @@ public class BlogBuilder {
try {
compressor.compress(path);
} catch (CompressorException e) {
System.err.printf("Exception compressing %s to %s: %s%n", path, compressor.getAlgorithm(), e.getMessage());
LOGGER.error("Error compressing {} to {}: {}", path, compressor.getAlgorithm(), e.getMessage());
}
}
}

View file

@ -1,14 +1,16 @@
package eu.m724.blog;
import org.apache.commons.cli.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.file.Path;
public class Main {
public static void main(String[] args) throws IOException {
System.out.println("Hello world!");
private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) throws IOException {
var commandLine = getCommandLine(args);
if (commandLine == null)
@ -37,12 +39,12 @@ public class Main {
builder.mkdirs(force);
System.out.println("---- START BUILD ----");
LOGGER.info("Building...");
builder.build();
System.out.println("----- END BUILD -----");
var end = System.nanoTime();
System.out.printf("Exported to %s (%.2f ms)\n", outputDirectory, (end - start) / 1000000.0);
// BAD
LOGGER.info("Exported to {} in {} ms", outputDirectory.toAbsolutePath(), "%.2f".formatted((end - start) / 1000000.0));
/* Server process */

View file

@ -2,6 +2,8 @@ package eu.m724.blog;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.SimpleFileServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.InetSocketAddress;
@ -13,6 +15,8 @@ import java.nio.file.Path;
* {@link SimpleFileServer} is used.
*/
public class Server {
private static final Logger LOGGER = LoggerFactory.getLogger(Server.class);
private final Path webroot;
private InetSocketAddress listenAddress;
@ -48,7 +52,7 @@ public class Server {
server.createContext("/", SimpleFileServer.createFileHandler(webroot.toAbsolutePath()));
server.start();
System.out.println("Server started on http:/" + server.getAddress());
LOGGER.info("Server started on http:/{}", server.getAddress());
this.listenAddress = server.getAddress();
}
@ -63,9 +67,10 @@ public class Server {
if (code != 0) {
throw new Exception("Exit code " + code);
}
System.out.println("Opened browser");
LOGGER.info("Opened browser"); // TODO make this debug?
} catch (Exception e) {
System.out.println("Failed to open browser: " + e);
LOGGER.error("Failed to open browser: {}", String.valueOf(e));
}
}
}

View file

@ -2,6 +2,8 @@ package eu.m724.blog.data;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.nio.file.Files;
@ -40,6 +42,8 @@ public record Post(
Map<String, String> custom,
String rawContent
) {
private static final Logger LOGGER = LoggerFactory.getLogger(Post.class);
/**
* Creates a {@link Post} instance by reading and parsing the content of a post file.
* <p>
@ -71,7 +75,7 @@ public record Post(
break;
if (properties.putIfAbsent(key, data) != null)
System.out.printf("Post %s: Duplicate property \"%s\". Only the first one will be used.\n", slug, key);
LOGGER.warn("[Post {}] Ignoring duplicate property: {}", slug, key);
}
var content = String.join("\n", lines).strip();
@ -122,7 +126,7 @@ public record Post(
}
} catch (GitAPIException e) {
draft = true;
System.out.printf("%s: Git exception, making draft: %s\n", slug, e.getMessage());
LOGGER.warn("[Post {}] Draft because of a Git exception: {}\n", slug, e.getMessage());
}
return new Post(slug, title, summary, draft, revisions, createdBy, createdAt, modifiedBy, modifiedAt, custom, content);

View file

@ -1,5 +1,7 @@
package eu.m724.blog.data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.snakeyaml.engine.v2.api.Load;
import org.snakeyaml.engine.v2.api.LoadSettings;
@ -13,6 +15,8 @@ import java.util.Map;
public record RenderOptions(
List<String> compress // TODO rename?
) {
private static final Logger LOGGER = LoggerFactory.getLogger(RenderOptions.class);
/**
* Creates a {@link Site} object by reading and parsing the configuration file at the specified path.<br>
* The configuration file must be a JSON file.
@ -31,12 +35,11 @@ public record RenderOptions(
var value = yaml.get(key);
switch (key) {
case "compress": {
case "compress":
compress = (List<String>) value;
}
default: {
System.out.println("Unrecognized option (render): " + key);
}
break;
default:
LOGGER.warn("Ignoring unrecognized render option: {}", key);
}
}

View file

@ -0,0 +1,11 @@
org.slf4j.simpleLogger.defaultLogLevel=info
# TODO maybe?
#org.slf4j.simpleLogger.showDateTime=true
#org.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss.SSS
org.slf4j.simpleLogger.showThreadName=false
org.slf4j.simpleLogger.showShortLogName=true
org.slf4j.simpleLogger.levelInBrackets=true
org.slf4j.simpleLogger.logFile=System.out