Giter VIP home page Giter VIP logo

spring-logging's Introduction

Spring logging

Logging library to be used with Spring Framework. Contains two mechanisms:

  • Logger injection
  • Logging aspects

Logger injection

In order to inject a logger instance into a spring managed bean use @Log annotation.

Example usage:

	import org.slf4j.Logger;
	import org.springframework.stereotype.Component;

	@Component
	public class BeanWithLogger {
		
		@Log
		private Logger logger;

		@Log(fromClass = SomeOther.class)
		private Logger logger2;
		
		/* ... */
		
	}

Configuration

To enable logger injection with @Log, simply register LogInjector in spring context.

Example using spring-java-config:

	import org.springframework.logging.inject.LogInjector;
	import org.springframework.context.annotation.Bean;
	import org.springframework.context.annotation.Configuration;

	@Configuration
	public class LogInjectorConfiguration {
		@Bean
		public LogInjector logInjector() {
			return new LogInjector();
		}
	}

Logger aspects

In order to log method invocations with aspects use @Logged annotation. You can use this annotation on methods and classes.

Example usage:

	import org.slf4j.Logger;
	import org.springframework.stereotype.Component;

	@Component
	@Logged
	public class BeanWithAspectLogging {

		public void sayHello() {
			/* ... */
		}

		public void sayHello(String hello) {
			/* ... */
		}

		public String sayHelloAndThrowNullPointerException(String hello) {
			logger.info("Inside method sayHelloReturnException. Hello: {}", hello);
			throw new NullPointerException("Test exception");
		}

	}

Configuration

To enable aspect logging just register LoggedAspect in spring context.

Example using spring-java-config:

	import org.springframework.logging.aspect.LoggedAspect;
	import org.springframework.context.annotation.Bean;
	import org.springframework.context.annotation.Configuration;
	import org.springframework.context.annotation.EnableAspectJAutoProxy;

	@Configuration
	@EnableAspectJAutoProxy(proxyTargetClass = true)
	public class LoggedAspectConfiguration {

		@Bean
		public LoggedAspect loggedAspect() {
			return new LoggedAspect();
		}

	}

Console output

Default console output for BeanWithAspectLogging#sayHelloAndThrowNullPointerException(String hello):

	2013-09-04 11:43:24,561 [TRACE] o.s.u.l.a.b.BeanTypeAnnotated:118 - >>> BeanTypeAnnotated.sayHelloReturnException(..): [xxx]
	/* Logs printed during method invocation */
	2013-09-04 11:43:24,582 [ERROR] o.s.u.l.a.b.BeanTypeAnnotated:134 - XXX Error occured: 
	/* Exception stack trace */
	2013-09-04 11:41:30,448 [TRACE] o.s.u.l.a.b.BeanTypeAnnotated:140 - <<< Returning BeanWithAspectLogging.sayHelloAndThrowNullPointerException(): null (14[ms])

Default console output can be overriden with custom implementation of MessageFormatter. Default logger provider mechanism can be overriden with custom implementation of LoggerProvider.

Maven dependency

In order to use this library add repository location into your pom.xml and add appropriate dependency.

	<dependency>
		<groupId>net.exacode.spring.logging</groupId>
		<artifactId>spring-aspect</artifactId>
		<version>${version.spring-logging}</version>
	</dependency>
	<!-- or -->
	<dependency>
		<groupId>net.exacode.spring.logging</groupId>
		<artifactId>spring-inject</artifactId>
		<version>${version.spring-logging}</version>
	</dependency>

Click here to lend your support to: Exacode open projects and make a donation at www.pledgie.com !

spring-logging's People

Contributors

pmendelski avatar

Watchers

 avatar  avatar

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.