Log4jResource { @PUT @Path ( "/update" ) @StatusCodes( { @ResponseCode( code = 200, condition = "Successfully update log level." ), @ResponseCode( code = 304, condition = "Log level is not modified." ) } ) @Produces( { MediaType.TEXT_PLAIN } ) public Response updateLogLevel( @FormParam( "level" ) String targetLevel, @FormParam( "category" ) String category ) throws Exception { LOGGER.setLevel( Level.INFO ); if ( StringUtils.isBlank( targetLevel ) && StringUtils.isBlank( category ) ) { return Response.notModified( "No parameter provided, log level not modified." ).build(); } Logger root = LogManager.getRootLogger(); if ( StringUtils.isNotBlank( targetLevel ) ) { LOGGER.info( "Request to set log level: " + targetLevel ); if ( StringUtils.isNotBlank( category ) ) { LOGGER.info( "Request to set log level for package: " + category ); Logger catLog = LogManager.exists( category ); if ( catLog != null ) { catLog.setLevel( Level.toLevel( targetLevel, root.getLevel() ) ); return Response.ok( "Setting log level for: '" + catLog.getName() + "' to be: " + catLog.getLevel() ).build(); } return Response.notModified( "Category: '" + Encode.forHtml( category ) + "' not found, log level not modified." ).build(); } root.setLevel( Level.toLevel( targetLevel, root.getLevel() ) ); LOGGER.info( "Root logger level set to: " + root.getLevel() ); Enumeration e = LogManager.getCurrentLoggers(); while ( e.hasMoreElements() ) { Logger logger = (Logger) e.nextElement(); logger.setLevel( Level.toLevel( targetLevel, root.getLevel() ) ); } } return Response.ok( "Log level updated." ).build(); }  @PUT @Path ( "/reload" ) @StatusCodes( { @ResponseCode( code = 200, condition = "Successfully reload from configuration." ) } ) @Produces( { MediaType.TEXT_PLAIN } ) Response reloadConfiguration(); @PUT @Path ( "/update" ) @StatusCodes( { @ResponseCode( code = 200, condition = "Successfully update log level." ), @ResponseCode( code = 304, condition = "Log level is not modified." ) } ) @Produces( { MediaType.TEXT_PLAIN } ) Response updateLogLevel( @FormParam(  "level" ) String targetLevel, @FormParam( "category" ) String category );  }
@Test public void updateLogLevel() throws Exception { Response res = target.updateLogLevel( null, null ); assertEquals( 304, res.getStatus() ); res = target.updateLogLevel( "debug", null ); int notDebug = 0; Enumeration e = LogManager.getCurrentLoggers(); while ( e.hasMoreElements() ) { Logger logger = (Logger) e.nextElement(); if ( !Level.DEBUG.equals( logger.getLevel() ) ) { notDebug++; } } assertEquals( 0, notDebug ); assertEquals( 200, res.getStatus() ); assertEquals( "Log level updated.", res.getEntity().toString() ); Level original = LogManager.getLogger( "org.pentaho" ).getLevel(); assertNotEquals( Level.ALL, original ); res = target.updateLogLevel( "ALL", "org.pentaho" ); assertEquals( Level.ALL, LogManager.getLogger( "org.pentaho" ).getLevel() ); assertEquals( 200, res.getStatus() ); assertEquals( "Setting log level for: 'org.pentaho' to be: ALL", res.getEntity().toString() ); res = target.updateLogLevel( "ALL", "foo.bar" ); assertEquals( 304, res.getStatus() ); res = target.updateLogLevel( "ALL", "<script>alert('XSS')</script>" ); assertEquals( 304, res.getStatus() ); assertEquals( "[\"Category: '&lt;script&gt;alert(&#39;XSS&#39;)&lt;/script&gt;' not found, log level not modified.\"]", res.getMetadata().values().toArray()[0].toString() ); }