log4j version 2

cameraegyptian_scribeI am a java programmer therefore I must know the most popular logging application. The apache log4j project version 2. Why is it so important to emphasize that it is version 2? Log4j2 differs from log4j a lot. It took two days to realize that it was an absolutely wrong way to watching tutorials of version one while I had version2. After I saw this I spent only twenty minutes to get a working example of logging with log4j version two. Yes, that is a link to my github store where you can see the code of this post.

 

The build.gradle file had changed a little since the previous post. It is better now because of the new options I added to it.

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'

mainClassName = "tryLogging.EntryPoint"

defaultTasks 'init'

def libDir = new File('lib/test')
def confDir = new File('conf')
def logDir = new File('log')

task makeConfDir << {
  confDir.mkdirs()
}

task makeLibDir << {
  libDir.mkdirs()
  logDir.mkdirs()
}

repositories {
  flatDir(dirs: 'lib')
  mavenCentral()
}

dependencies {
  testCompile fileTree('lib/test')
  compile 'joda-time:joda-time:2.4'
  compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.0.2'
  compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.0.2'
}

task init {
  dependsOn 'makeLibDir'
  dependsOn 'makeConfDir'
  dependsOn 'copyTask'
  dependsOn 'copyConfTask'
  dependsOn 'initSourceFolders'
  dependsOn 'eclipse'
  dependsOn check
}

task initSourceFolders {
  description 'creates std file structure for java, test and resources'
  sourceSets*.java.srcDirs*.each { it.mkdirs() }
  sourceSets*.resources.srcDirs*.each { it.mkdirs() }
}

sourceSets.main.java.srcDir 'conf'

jar {
  description 'creates jar; basename: testingGradle, version: 0.0.77'
  baseName = 'log4jTrying'
  version = '0.0.1'
}

task copyTask(type: Copy) {
  description 'something has to be inside src and lib dirs because of behaviour of git'
  from '../deps'
  into 'lib'
  include '**/*.jar'
  exclude '**/*.txt'
}

task copyConfTask(type: Copy) {
  description 'copies log4j2 xml to conf dir'
  from '../confs'
  into 'conf'
  include 'log4j2.xml'
}

task trigger(dependsOn: 'build') {
  description 'this is the default task in this project. This task calls (depends on) build task'
}

task delMyDirX(type: Delete) {
  delete fileTree(dir: 'myOwnDstDir', include: '*.xml')
  description 'deletes all xml files in myOwnDstDir directory'
}

task delMyDir(type: Delete) {
  delete fileTree(dir: 'myOwnDstDir', exclude: 'dont_delete.xml')
  description 'deletes all files in myOwnDstDir except dont_delete.xml'
}

task delToInit(type: Delete) {
  delete fileTree(dir: 'build')
  delete 'src/main/java/tryLogging'
  delete 'src/test/java/tryLogging'
  delete 'bin'
}

task cleanAllMyDir(type: Delete) {
  delete fileTree(dir: 'myOwnDstDir')
  description 'deletes all files in myOwnDstDir'
}

delToInit.dependsOn cleanEclipse

This is how you can create directories using gradle:

def confDir = new File('conf')

task makeConfDir << {
  confDir.mkdirs()
}

The trigger is:

gradle makeConfDir

Or when

defaultTask makeConfDir

Then it is enough to write simply

gradle

Another useful snippet responsible for the source file structure:

task initSourceFolders {
  description 'creates std file structure for java, test and resources'
  sourceSets*.java.srcDirs*.each { it.mkdirs() }
  sourceSets*.resources.srcDirs*.each { it.mkdirs() }
}

Without the following line you have to add conf dir to source folders group in eclipse again and again after every gradle refresh!

sourceSets.main.java.srcDir 'conf'

I can remove directories from now:

task delMyDirX(type: Delete) {
  delete fileTree(dir: 'myOwnDstDir', include: '*.xml')
  description 'deletes all xml files in myOwnDstDir directory'
}

The code itself is simple:
The main method…

package tryLogging;
public class EntryPoint
{
  private static final String THE_MESSAGE = "Thanks a lot for reading develoSapiens.blog.com!";
  public static void main( String[] args )
  {
    MyLogger myLogger = new MyLogger();
    myLogger.justSayIt( THE_MESSAGE );
  }
}

and the logger:

package tryLogging;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class MyLogger
{
  private static final Logger logger = LogManager.getLogger( MyLogger.class.getName() );
  private static final String LOGLEVEL_HEADER_FATAL = "This is fatal level, and the message is: ";
  private static final String LOGLEVEL_HEADER_ERROR = "This is error level, and the message is: ";
  private static final String LOGLEVEL_HEADER_WARN = "This is warning level, and the message is: ";
  private static final String LOGLEVEL_HEADER_INFO = "This is info level, and the message is: ";
  private static final String LOGLEVEL_HEADER_DEBUG = "This is debug level, and the message is: ";
  private static final String LOGLEVEL_HEADER_TRACE = "This is trace level, and the message is: ";

  public void justSayIt( String toSay )
  {
    logger.fatal( LOGLEVEL_HEADER_FATAL + toSay );
    logger.error( LOGLEVEL_HEADER_ERROR + toSay );
    logger.warn( LOGLEVEL_HEADER_WARN + toSay );
    logger.info( LOGLEVEL_HEADER_INFO + toSay );
    logger.debug( LOGLEVEL_HEADER_DEBUG + toSay );
    logger.trace( LOGLEVEL_HEADER_TRACE + toSay );
  }
}

Advertisements
This entry was posted in Frameworks and tagged , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s