Logging - Conditional printing

From Juneday education
Jump to: navigation, search

Description

This chapter aims to make you think about conditional printing - turning log printing on and off, and using different log levels. It is not a tutorial of Java's Logging technology, but rather shows you step by step how to create your own loggin framework, starting with a simple print method which checks a global boolean in order to decide whether to print or not. Step by step we add the following features:

  • Simple boolean check whether to print
  • The check depends on user-supplied flag using java -DDO_DEBUG as the flag
  • Letting the debug/log method create the tag (what class, what method and on what line was the call to log()?)
  • Introducing log levels - the log level decides if certain log methods should print or not
  • Making the logger into an object rather than a static method
  • Using an enum for the Log Levels
  • Equipping the enum with a parse() factory method
  • Using user-supplied flag for the LOG_LEVEL and pass it to the enum's parse() method
  • Equipping the Logger class with a constructor for both Level and PrintStream to use
  • A discussion on what else could be done, and what could be done differently
  • Some pointers to existing logging frameworks

Chapter links

Vimeo channel

Source code

Books this chapter is a part of

Further reading

Book TOC | previous chapter | Todo:next chapter