如何创建Java Mixins: @slf4j

发布于 2025-01-28 07:34:54 字数 291 浏览 5 评论 0原文

我正在尝试创建Java Mixin并使用@Slf4j注释。但是,Intellij显示了一个错误@slf4j仅适用于类和枚举

import lombok.extern.slf4j.Slf4j;

@Slf4j
public interface Foo {
    default void foo() {
        log.info("Hello world");
    }
}

I'm trying to create a Java mixin and use the @Slf4j annotation. However intellij shows an error @Slf4j is only legal for classes and enums.

import lombok.extern.slf4j.Slf4j;

@Slf4j
public interface Foo {
    default void foo() {
        log.info("Hello world");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

芸娘子的小脾气 2025-02-04 07:34:54

如果要使用记录界面的记录,则可以使用一个常数:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public interface Foo {
    Logger log = LoggerFactory.getLogger(Foo.class);

    default void foo() {
        log.info("Hello from Foo");
    }
}

如果该界面具有某些实现,并且可以覆盖一个记录器:

public class Bar implements Foo {
    Logger log = LoggerFactory.getLogger(Bar.class);

    public void bar() {
        log.info("Hello from Bar");
    }
}

使用Lombok,可以更简单地完成:

@Slf4j
class Bar implements Foo {
    public void bar() {
        log.info("Hello from Bar");
    }
}

demo代码:

public class LoggerDemo {
    public static void main(String[] args) {
        Bar bar = new Bar();
        bar.foo();
        bar.bar();
    }
}

upputs:

20:40:48.010 [main] INFO demo.Foo - Hello from Foo 
20:40:48.014 [main] INFO demo.Bar - Hello from Bar

If you want to use logging for the interface you could use a constant:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public interface Foo {
    Logger log = LoggerFactory.getLogger(Foo.class);

    default void foo() {
        log.info("Hello from Foo");
    }
}

If will have some implementation for this interface and you could overwrite a logger:

public class Bar implements Foo {
    Logger log = LoggerFactory.getLogger(Bar.class);

    public void bar() {
        log.info("Hello from Bar");
    }
}

With Lombok it could be done even simpler:

@Slf4j
class Bar implements Foo {
    public void bar() {
        log.info("Hello from Bar");
    }
}

Demo code:

public class LoggerDemo {
    public static void main(String[] args) {
        Bar bar = new Bar();
        bar.foo();
        bar.bar();
    }
}

Output:

20:40:48.010 [main] INFO demo.Foo - Hello from Foo 
20:40:48.014 [main] INFO demo.Bar - Hello from Bar
此岸叶落 2025-02-04 07:34:54

解决该问题的一种简单方法是将记录器传递到foo()方法:

import org.slf4j.Logger;

public interface Foo {
    default void foo(Logger log) {
        log.info("Hello world");
    }
}

然后在实现foo @slf4j注释。 >::

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Bar implements Foo {
    public void bar() {
        foo(log);
    }
}

A simple way to solve the issue is to pass the logger to the foo() method:

import org.slf4j.Logger;

public interface Foo {
    default void foo(Logger log) {
        log.info("Hello world");
    }
}

Then use the @Slf4j annotation on classes that implement Foo:

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Bar implements Foo {
    public void bar() {
        foo(log);
    }
}
﹉夏雨初晴づ 2025-02-04 07:34:54

一个选项是将getLogger方法添加到接口,并要求实现该方法。然后接口可以调用该方法。

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;

public interface Foo {

    Logger getLogger();

    default void foo() {
        getLogger().info("Hello world");
    }
}

@Slf4j
public class Bar implements Foo {

    public void bar() {
        foo();
    }

    @Override
    public Logger getLogger() {
        return log;
    }
}

One option is to add a getLogger method to the interface and require the implementations to implement that method. Then the interface can call that method.

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;

public interface Foo {

    Logger getLogger();

    default void foo() {
        getLogger().info("Hello world");
    }
}

@Slf4j
public class Bar implements Foo {

    public void bar() {
        foo();
    }

    @Override
    public Logger getLogger() {
        return log;
    }
}
无可置疑 2025-02-04 07:34:54

一种选项是使用静态getLogger方法创建内部类。

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;

public interface Foo {

    @Slf4j
    class Log {
        public static Logger getLogger() {
            return log;
        }
    }

    default void foo() {
        Log.getLogger().info("Hello world");
    }
}

One option is to create an inner class with a static getLogger method.

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;

public interface Foo {

    @Slf4j
    class Log {
        public static Logger getLogger() {
            return log;
        }
    }

    default void foo() {
        Log.getLogger().info("Hello world");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文