如何从服务中调用 log.error()?总是给出“No such 属性: log for class: gi.mypackage.$UserServiceImplementation

How to call log.error() from a service? Always gives "No such property: log for class: gi.mypackage.$UserServiceImplementation

在 Grails 3.3.8 中,使用基于接口的新 Grails 服务,我的 UserService 是这样的:

interface IUserService {
    User get(Serializable id)
    List<User> list(Map args)
    Long count()
    void delete(Serializable id)
    User save(User user)
}

@Service(User)
@Transactional
abstract class UserService implements IUserService {
    @Override
    User save(User user) {
        log.error("mymessage")
}

当我调用 userService.save(user) 方法时,它无法解析 log。它从开箱即用的控制器开始工作。如何将日志实例添加到 Grails 服务中?

错误是:

No such property: log for class: gi.mypackage.$UserServiceImplementation

基于文档部分 logger names, services in grails-app/services/ should have a log automatically injected. However, if your service is elsewhere, like src/main/groovy/, you need to add the @Slf4j 对您的服务的注释 class。

@Service(User)
@Transactional
@Slf4j
abstract class UserService implements IUserService {
    @Override
    User save(User user) {
        log.error("mymessage")
}