简体   繁体   中英

Should I log CRUD methods in Service layer?

I started add slf4j logging an custom exception into my project. I add logs into my ServiceImplementation class where I have CRUD methods which I implement in my DaoImplementation class. The question is: If my CRUD methods already have added logs and custom exception in Dao class should they have same logs in other class? Example:

public class SpringTeacherDao implements TeacherDao {
private static Logger logger = LoggerFactory.getLogger(SpringTeacherDao.class);

private JdbcTemplate jdbcTemplate;


@Autowired
public SpringTeacherDao(DataSource dataSource) {
    jdbcTemplate = new JdbcTemplate(dataSource);
}

@Override
public void create(Teacher teacher) {
    logger.debug("Add teacher status: in progress...");
    if (teacher == null) {
        String error = "Add teacher status: Error, teacher is null.";
        logger.warn(error);
        throw new UniversityDaoException(error);
    }
    String sql = "INSERT INTO teacher VALUES(?,?,?)";
    try {
        jdbcTemplate.update(sql, teacher.getID(), teacher.getName(), teacher.getSurname());
    }catch (DuplicateKeyException e){
        String duplicate = "Teacher id already exist";
        logger.warn(duplicate);
        throw new UniversityDaoException(duplicate);
    }
    logger.info("Add teacher status: Teacher has been added");
}
public class TeacherServiceImpl implements TeacherService {
private static Logger logger = LoggerFactory.getLogger(TeacherServiceImpl.class);
private final TeacherDao teacherDao;
private final LectureDao lectureDao;
private final TeachersLectureDao teachersLectureDao;


public TeacherServiceImpl(TeacherDao teacherDao, LectureDao lectureDao, TeachersLectureDao teachersLectureDao) {
    this.teacherDao = teacherDao;
    this.lectureDao = lectureDao;
    this.teachersLectureDao = teachersLectureDao;
}

@Override
public void addTeacher(Teacher teacher) {
    teacherDao.create(teacher);
}

It's not a java related question, but rather coding practices question. You may get different answers from each person that you ask, as it may be related to standards established in the community/company/personal preference etc, therefore: don't expect a single answer.

My perspective on the subject (just logging exception on multiple layers, I'm not talking about propagating/rethrowing/wrapping an exception - as that's another story) is as follows:

If given layer has anything specific to add to the log: log it. For instance:

  • DAO layer may log information related to DB failure for the exception
  • Service layer (calling that DAO) may have more context in which that operation executed (ie some user id, or operation specific data) that could be logged with the exception.
  • There could be more layers, ie next one could be some Controller, that can log ie some request identifier (for tracing purposes) or maybe request data.

Of course it would cause the exception to be logged 3 times, each one would add more data to the log. However, when you write DAO class, you have no guarantee if the service will log an exception or not, therefore it should always log it (unless you have a global exception handler that would log all uncaught exceptions - but then: you have no guarantee that a service didn't just catch an exception without logging)

BTW - a rule for rethrowing an exception: Always embed a cause exception in the new exception that you throw, this way you'll see a full chain of exceptions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM