Skip to content

CustomLogger

Flow Arg edited this page Jul 6, 2021 · 4 revisions

Custom Logger

In this tutorial, you will learn how to use a custom logger on FlowUpdater.

Introduction

ILogger is the basic class of the FlowMultitools Logger API. Logger is the default implementation of the FlowMultitools Logger API.

Everyone can create a custom implementation of the Logger API by creating a class implementing ILogger (or extending Logger if you want to keep some methods).

Example Usage

This is a possible implementation's example:

import java.nio.file.Path;

public class MyLogger extends Logger
{
    private Path path;
    private String prefix;
    
    public MyLogger(Path path, String prefix)
    {
        this.path = path;
        this.prefix = prefix;
    }

    @Override
    public void err(String message)
    {
        System.err.println(this.prefix + " ERR: " + message);
    }

    @Override
    public void info(String message)
    {
        System.out.println(this.prefix + " INFO: " + message);
    }

    @Override
    public void warn(String message)
    {
        System.out.println(EnumLogColor.YELLOW + this.prefix + " WARN: " + message + EnumLogColor.RESET);
    }

    @Override
    public void debug(String message)
    {
        System.out.println(EnumLogColor.CYAN + this.prefix + " DEBUG: " + message + EnumLogColor.RESET);
    }

    @Override
    public void infoColor(EnumLogColor color, String message)
    {
        this.info(color + this.prefix + " INFO: " + message + EnumLogColor.RESET);
    }

    @Override
    public Path getLogPath()
    {
        return this.path;
    }

    @Override
    public void setLogPath(Path logPath)
    {
        this.path = logPath;
    }

    @Override
    public String getPrefix()
    {
        return this.prefix;
    }
}

Check all available methods in ILogger for more customization and Logger for usage.

Then, just add an instance of your custom logger as a FlowUpdater's argument:

import fr.flowarg.flowlogger.ILogger;
import fr.flowarg.flowupdater.FlowUpdater;

import java.nio.file.Paths;

class Test {
    public static void update() {
        ILogger logger = new MyLogger(Paths.get("updater.log"), "Tutorial~");
        FlowUpdater flowUpdater = new FlowUpdater.FlowUpdaterBuilder()
                .withLogger(logger)
                // add all arguments you need
                .build();
    }
}

That's all! Enjoy using FlowUpdater!

Clone this wiki locally