Coder Social home page Coder Social logo

action-logging's Introduction

Action Logging - Grails Plugin

A simple Grails Plugin to save a controller actions log 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 saves 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/

Grails Version

2.* >

Install

Repository:

mavenRepo "http://dl.bintray.com/erickmorales3/maven"

Dependency:

compile ":action-logging:1.0.0"

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

Printing custom log messages like println function

By default custom messages are only visible in Action Logging Event List screen or in action_loging database table, but is possible enable pirntln function adding the @PrintCustomLog annotation:

  • To all controller actions
@ActionLogging
@PrintCustomLog
class SampleController {
    def actionLoggingService
    
    def index(){
        actionLoggingService.log("A custom log message 1 - printed")
        actionLoggingService.log("A custom log message 2 - printed")
    }
}
  • To all controller actions except to specific one
@ActionLogging
@PrintCustomLog
class SampleController {
    def actionLoggingService
    
    def index(){
        actionLoggingService.log("A custom log message 1 - printed")
        actionLoggingService.log("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
@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("A custom log message 3 - printed")
        actionLoggingService.log("A custom log message 4 - printed")
    }
}

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

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

Setting action type to specific action

  • 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.

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

I hope it helps you

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.