View on GitHub

Action Logging - Grails Plugin

Simplest debugging and better application security

Download this project as a .zip file Download this project as a tar.gz file

Action Logging - Grails Plugin

Store detailed log of controller actions in database including controller name, action name, start-end time, total time (seconds), exceptions, custom messages and more.

Description

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.

Please see https://grails.org/plugin/action-logging

Grails Version

2.* >

Install

Dependency:

compile ":action-logging:1.1.1"

How to use

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

alt tag

Some examples

Enable Action Logging to all controller actions except to specific one

@ActionLogging
class SampleController {

    def index(){

    }

    @ActionLogging(false)
    def methodWithoutActionLogging(){

    }
}

Enable Action Logging to specific controller action

class SampleController {

    def index(){

    }

    @ActionLogging
    def methodWithActionLogging(){

    }
}

Adding custom log messages

Inject actionLoggingService class:

@ActionLogging
class SampleController {
    def actionLoggingService

    def index(){
        actionLoggingService.log("A custom log message 1")
        actionLoggingService.log("A custom log message 2")
    }
}

Managing custom log messages with Logger implementation

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:

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")
    }
}
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")
    }
}
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.

Setting action type to all controller actions

@ActionLogging
@ActionType("Administrator/Supervisor Actions")
class SampleController {

}

Identifying current user with Spring Security Core Plugin

@ActionLogging
@SpringUserIdentification
class SampleController {

}

Identifying current user without Spring Security Core Plugin

@ActionLogging
class SampleController {
    def actionLoggingService

    def index(){
        actionLoggingService.setUserId(3)
    }
}

Setting custom action name

@ActionLogging
class SampleController {
    @CustomActionName("Custom action name")
    def index(){

    }
}
@ActionLogging
class SampleController {
    def actionLoggingService

    def index(){
        actionLoggingService.setCustomActionName("Custom action name")
    }
}

Setting action type to specific action

@ActionLogging
class SampleController {
    @ActionType("Administrator Action")
    def index(){

    }
}
@ActionLogging
class SampleController {
    def actionLoggingService

    def index(){
        actionLoggingService.setActionType("Administrator Action")
    }
}

It overrides action type defined in class declaration.

Setting handled exception in try catch block

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)
        }
    }
}

License

MIT License

Thanks