Store detailed log of controller actions in database including controller name, action name, start-end time, total time (seconds), exceptions, custom messages and more.
It's specially useful to developers and system administrators, because it store custom messages, exceptions and execution time. This allows to know application weaknesses to correct it before user report. In addition, it allows to identify the current user to create a user action history.
For further information see https://erickmorales3.github.io/action-logging/
2.* >
Dependency:
compile ":action-logging:1.1.1"
Import some annotation classes on the controller where you want to enable Action Logging.
import org.mirzsoft.grails.actionlogging.annotation.*
Add @ActionLogging annotation
@ActionLogging
class SampleController {
}
This way, will store a log of all existing actions in controller and the result can be seen navigating to http://your/app/path/actionLoggingEvent/index
@ActionLogging
class SampleController {
def index(){
}
@ActionLogging(false)
def methodWithoutActionLogging(){
}
}
class SampleController {
def index(){
}
@ActionLogging
def methodWithActionLogging(){
}
}
Inject actionLoggingService class:
@ActionLogging
class SampleController {
def actionLoggingService
def index(){
actionLoggingService.log("A custom log message 1")
actionLoggingService.log("A custom log message 2")
}
}
By default custom messages are only visible in Action Logging Event List screen or in action_loging database table, but is possible manage log levels and incorporate it with your own log implementation like logback, log4j, etc. Adding the @PrintCustomLog annotation and importing LoggingLevel class:
- To all controller actions
import org.mirzsoft.grails.actionlogging.Constants.LoggingLevel
@ActionLogging
@PrintCustomLog
class SampleController {
def actionLoggingService
def index(){
actionLoggingService.log(LoggingLevel.INFO, "A custom log message 1 - printed")
actionLoggingService.log(LoggingLevel.INFO, "A custom log message 2 - printed")
}
}
- To all controller actions except to specific one
import org.mirzsoft.grails.actionlogging.Constants.LoggingLevel
@ActionLogging
@PrintCustomLog
class SampleController {
def actionLoggingService
def index(){
actionLoggingService.log(LoggingLevel.INFO, "A custom log message 1 - printed")
actionLoggingService.log(LoggingLevel.INFO, "A custom log message 2 - printed")
}
@PrintCustomLog(false)
def methodWithoutCustomPrint(){
actionLoggingService.log("A custom log message 3")
actionLoggingService.log("A custom log message 4")
}
}
- To specific controller action
import org.mirzsoft.grails.actionlogging.Constants.LoggingLevel
@ActionLogging
class SampleController {
def actionLoggingService
def index(){
actionLoggingService.log("A custom log message 1")
actionLoggingService.log("A custom log message 2")
}
@PrintCustomLog
def methodWithCustomPrint(){
actionLoggingService.log(LoggingLevel.INFO, "A custom log message 3 - printed")
actionLoggingService.log(LoggingLevel.INFO, "A custom log message 4 - printed")
}
}
Available Logging levels: TRACE, DEBUG, INFO, WARN, ERROR.
Note: If level is not specified, custom messages will printed using println function.
@ActionLogging
@ActionType("Administrator/Supervisor Actions")
class SampleController {
}
@ActionLogging
@SpringUserIdentification
class SampleController {
}
@ActionLogging
class SampleController {
def actionLoggingService
def index(){
actionLoggingService.setUserId(3)
}
}
- Using @CustomActionName annotation
@ActionLogging
class SampleController {
@CustomActionName("Custom action name")
def index(){
}
}
- Using actionLoggingService class
@ActionLogging
class SampleController {
def actionLoggingService
def index(){
actionLoggingService.setCustomActionName("Custom action name")
}
}
- Using @ActionType annotation
@ActionLogging
class SampleController {
@ActionType("Administrator Action")
def index(){
}
}
- Using actionLoggingService class
@ActionLogging
class SampleController {
def actionLoggingService
def index(){
actionLoggingService.setActionType("Administrator Action")
}
}
It overrides action type defined in class declaration.
The handled exception in try catch block are omited, but is posible setting an exception object manually, as follows:
@ActionLogging
class SampleController {
def actionLoggingService
def index(){
try {
def a = 1 / 0
} catch (ex) {
actionLoggingService.setCustomException(ex)
}
}
}
MIT License
- @burtbeckwith
- @rpalcolea