I want to write some info logs that include executor info. Let's say I have this SaveUser function:
public class UserDAO {
Logger logger = LogManager.getLogger();
public void SaveUser(User user, LoggerInfo info) {
logger.info("Executor {} trying to save user {}...", "Executor name comes here", user.name);
this.saveToFirstDB(user);
this.saveToSecondDB(user);
logger.info("Executor {} succesfully saved user {}.", "Executor name comes here", user.name);
}
private void SaveToFirstDB(User user) {
logger.info("Executor {}, enter SaveToFirstDB.", "Executor name comes here", user.name);
// save user to first db..
logger.info("Executor {}, exit SaveToFirstDB.", "Executor name comes here", user.name);
}
private void SaveToSecondDB(User user) {
logger.info("Executor {}, enter SaveToSecondDB.", "Executor name comes here", user.name);
I
// save user to second db..
logger.info("Executor {}, exit SaveToSecondDB.", "Executor name comes here", user.name);
}
}
And I want to call it at the Controller layer:
public class UserController{
// for example - using Java Spark request and response context
public void SaveUser(Request req, Response res) {
User userToSave = new User(.....); // creating user object from request params
UserDAO userDAO = new UserDAO();
userDAO.SaveUser(userToSave);
// I want to use this executor name:
System.out.println(req.attribute("executor"));
}
}
Of course I can inject the executor name, but I do not think I should do that (this is just a simple example, I want to inject more details like date, role, etc..).
I do not want to inject the details, and I do not want the DAL (for example) to know about the context object of the Controller layer (Play, Spark..). So what is the correct way to write informative logs at backend?
Aucun commentaire:
Enregistrer un commentaire