Log4j Java: Java Applications වල Logging කරන හැටි SC Guide

Log4j Java: Java Applications වල Logging කරන හැටි SC Guide

Introduction to Log4j in Java: SC Guide

කට්ටියම කොහොමද? ඔන්න අදත් ඔයාලා වෙනුවෙන් අලුත්ම technical article එකක් අරගෙන ආවා. අපි හැමෝටම සමහර වෙලාවට එන ප්‍රශ්නයක් තමයි, develop කරපු software එකක bug එකක් ආවම, ඒක කොහෙන්ද ආවේ, මොකක්ද මේකට හේතුව කියලා හොයාගන්න බැරි වෙන එක. ඒක හරියට CCTV නැති තැනක හොරකමක් වුනා වගේ තමයි. ඔයාලා හිතලා තියෙනවද අපේ application එකේ හැම වෙලේම මොකද වෙන්නේ කියලා බලන්න පුළුවන් වුනා නම් හොඳයි නේද කියලා? ඔන්න ඔය වගේ වෙලාවට තමයි logging කියන සංකල්පය අපිට පිහිටට එන්නේ. විශේෂයෙන්ම Java applications වල, මේ logging කියන්නේ නැතුවම බැරි දෙයක්. කෝපි කෝප්පයක් එහෙම නැත්තම් තේ කෝප්පයක් එහෙම බීලා එන්න, මොකද අද කතා කරන්න යන්නේ Java වල debugging සහ monitoring වලට නැතුවම බැරි tool එකක් ගැන – ඒ තමයි Log4j! අද අපි Log4j කියන්නේ මොකක්ද, ඒක අපේ projects වලට add කරගන්නේ කොහොමද, আর ඒකෙන් ප්‍රයෝජන ගන්නේ කොහොමද කියලා විස්තරාත්මකව කතා කරමු.

Log4j කියන්නේ මොකක්ද?

සරලවම කියනවා නම්, Log4j කියන්නේ Apache Software Foundation එකෙන් හදපු, Java applications වලට logging පහසුකම් සපයන, open-source සහ extremely powerful framework එකක්. මේක අපිට අපේ application එක run වෙන අතරතුරදී සිදුවෙන දේවල්, එනම් errors, warnings, information, debug messages වගේ දේවල් record කරලා තියාගන්න උදව් වෙනවා. අපි සාමාන්‍යයෙන් System.out.println() use කරනවනේ console එකට messages print කරන්න. ඒත් System.out.println() වලට වඩා Log4j ගොඩක් වාසිදායකයි. ඇයි ඒ?

  • ලඝුකරණ මට්ටම් (Logging Levels): Log4j එකේ TRACE, DEBUG, INFO, WARN, ERROR, FATAL වගේ logging levels තියෙනවා. අපිට අවශ්‍ය මට්ටමේ messages විතරක් record කරන්න මේකෙන් පුළුවන්.
  • නම්‍යශීලී Appenders: අපිට messages console එකට, file එකකට, database එකකට, එහෙමත් නැත්තම් remote server එකකට වුනත් record කරන්න පුළුවන්. ඒක අපිට අවශ්‍ය විදිහට customize කරන්න පුළුවන්.
  • Performance: System.out.println() වලට වඩා Log4j ගොඩක් efficient. විශේෂයෙන්ම high-volume applications වලදී මේකේ වෙනස හොඳටම තේරෙයි.
  • Configuration: අපිට logging configuration එක code එක ඇතුළේ වෙනස් නොකර, external configuration file එකක් (like log4j2.xml or log4j2.properties) මගින් පාලනය කරන්න පුළුවන්.

මේ වගේ වාසි ගොඩක් තියෙන නිසා තමයි Log4j කියන්නේ production-ready applications වලට අත්‍යවශ්‍ය tool එකක් වෙලා තියෙන්නේ.

Log4j Setup කරගමු! (Maven/Gradle)

හරි, දැන් අපි දැනගත්තා Log4j කියන්නේ මොකක්ද කියලා. දැන් බලමු මේක අපේ Java project එකකට add කරගන්නේ කොහොමද කියලා. ඔයාලා Maven හරි Gradle හරි use කරනවා නම්, මේක ගොඩක් ලේසියි.

Maven Project එකකට Log4j Add කරන හැටි:

ඔයාලගේ pom.xml file එකේ <dependencies> section එකට මේ ටික add කරන්න. Log4j 2 series එකේ latest stable version එක use කරන්න මතක තියාගන්න. මම මෙතන 2.20.0 version එක දාලා තියෙනවා.


<dependencies>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.20.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.20.0</version>
    </dependency>
</dependencies>

Gradle Project එකකට Log4j Add කරන හැටි:

ඔයාලගේ build.gradle file එකේ dependencies section එකට මේ ටික add කරන්න.


dependencies {
    implementation 'org.apache.logging.log4j:log4j-api:2.20.0'
    implementation 'org.apache.logging.log4j:log4j-core:2.20.0'
}

dependencies add කරාට පස්සේ, project එක clean කරලා build කරන්න. දැන් Log4j libraries ටික ඔයාලගේ project එකට add වෙලා තියෙයි.

ඊළඟට අපිට අවශ්‍ය වෙනවා Log4j වලට configuration file එකක්. සාමාන්‍යයෙන් මේක src/main/resources folder එක ඇතුළේ log4j2.xml නමින් හදනවා. මෙතන මම පොඩි basic log4j2.xml configuration එකක් දානවා. මේක console එකටයි, file එකකටයි log කරන විදිහට හදලා තියෙන්නේ.


<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="ConsoleAppender" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
        <File name="FileAppender" fileName="logs/application.log">
            <PatternLayout pattern="%d %p %c{1.} [%t] %m%n"/>
        </File>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="ConsoleAppender"/>
            <AppenderRef ref="FileAppender"/>
        </Root>
    </Loggers>
</Configuration>

මේ configuration file එකේ අපි Appenders දෙකක් define කරලා තියෙනවා:

  • ConsoleAppender: console එකට messages print කරන්න.
  • FileAppender: logs/application.log කියන file එකට messages save කරන්න.

PatternLayout එකෙන් අපි කියනවා log message එකේ format එක මොන වගේද කියලා. උදාහරණයක් විදිහට %d කියන්නේ date එක, %p කියන්නේ log level එක, %c කියන්නේ logger name එක වගේ දේවල්.

Loggers section එකේ Root logger එකේ level එක info විදිහට දාලා තියෙනවා. ඒ කියන්නේ INFO level එකේ ඉඳන් උඩට තියෙන messages (INFO, WARN, ERROR, FATAL) මේ Appenders දෙකටම යවන්න කියලා.

Log Levels සහ Appenders

Log4j වලදී logging කියන දේ තේරුම් ගන්න මේ Log Levels සහ Appenders ගැන හොඳ අවබෝධයක් තියෙන්න ඕනේ.

Log Levels:

Log Levels කියන්නේ අපි record කරන message එකේ වැදගත්කම හඳුන්වන්න use කරන මට්ටම්. පහළ ඉඳන් උඩට වැදගත්කම වැඩි වෙනවා.

  • TRACE: මේක තමයි low level එකේම තියෙන level එක. ගොඩක් විස්තරාත්මක messages. Debugging වලට විතරක් වගේ use කරනවා.
  • DEBUG: Debugging වලදී අවශ්‍ය වෙන granular debugging information. Development environment එකේදී use කරනවා.
  • INFO: Application එකේ සාමාන්‍ය ක්‍රියාකාරිත්වය පෙන්නන messages. Production environment එකේදී මේ level එකේ messages record කරනවා.
  • WARN: Application එකට හානියක් නැති, නමුත් අවධානය යොමු කළ යුතු විය හැකි අවස්ථා. (e.g., Deprecated API usage, potential performance issues).
  • ERROR: Serious errors. Application එකේ කොටසක් හෝ සම්පූර්ණයෙන්ම ක්‍රියා විරහිත විය හැකි අවස්ථා. (e.g., Exceptions).
  • FATAL: Very severe errors that cause the application to abort. Application එක සම්පූර්ණයෙන්ම ක්‍රියා විරහිත වෙන අවස්ථා.

අපේ log4j2.xml එකේ Root level="info" කියලා තිබ්බොත්, INFO, WARN, ERROR, FATAL level messages විතරයි record වෙන්නේ. DEBUG, TRACE messages ignore වෙනවා.

Appenders:

Appenders කියන්නේ log messages කොහෙටද යවන්නේ කියලා කියන එක. අපි කලින් දැක්කා වගේ, console එකට, file එකකට යවන්න පුළුවන්. තව ගොඩක් appenders තියෙනවා:

  • ConsoleAppender: Console එකට logs print කරන්න.
  • FileAppender: Single file එකකට logs save කරන්න.
  • RollingFileAppender: Logs වල size එකක් හරි, කාලයක් හරි ගියාම අලුත් file එකකට logs save කරනවා. (Production applications වලට අත්‍යවශ්‍යයි).
  • DatabaseAppender: Database එකකට logs save කරන්න.
  • SocketAppender: Remote server එකකට logs යවන්න.

මේවා ගැන වැඩිදුරටත් Log4j documentation එක බලන්න පුළුවන්. අපිට අවශ්‍ය විදිහට Appenders හදන්න පුළුවන්. මේ තුනම එකට එකතු වෙලා තමයි logging process එක සිද්ධ වෙන්නේ: Logger -> Filter -> Appender -> Layout.

ප්‍රායෝගිකව Log4j භාවිතය

හරි, දැන් අපි theory එකයි setup එකයි බැලුවා. දැන් බලමු අපේ Java code එක ඇතුළේ Log4j use කරන්නේ කොහොමද කියලා.

පළමුවෙන්ම අපිට අවශ්‍ය වෙනවා org.apache.logging.log4j.LogManager class එකෙන් Logger instance එකක් ගන්න. මේක සාමාන්‍යයෙන් class එකක static final field එකක් විදිහට define කරනවා.

මෙන්න පොඩි code example එකක්:


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

public class MyApplication {

    // Logger instance එක define කරනවා
    private static final Logger logger = LogManager.getLogger(MyApplication.class);

    public static void main(String[] args) {
        logger.trace("This is a TRACE message.");
        logger.debug("This is a DEBUG message.");
        logger.info("This is an INFO message - Application started.");

        try {
            int result = divide(10, 0); // මෙතන ArithmeticException එකක් එනවා
            logger.info("Result of division: " + result);
        } catch (ArithmeticException e) {
            logger.error("An error occurred during division: ", e); // Exception එකත් එක්ක log කරනවා
        }

        logger.warn("This is a WARN message - Potential issue.");
        logger.fatal("This is a FATAL message - Application is shutting down due to a critical error.");

        logger.info("Application finished.");
    }

    public static int divide(int a, int b) {
        if (b == 0) {
            // අපි custom exception එකක් throw කරමු
            throw new ArithmeticException("Cannot divide by zero");
        }
        return a / b;
    }
}

මේ code එක run කරාම, ඔයාලා කලින් හදපු log4j2.xml configuration එකට අනුව messages console එකේයි, logs/application.log file එකේයි save වෙයි. Root level එක info විදිහට දාලා තියෙන නිසා TRACE සහ DEBUG messages console එකේවත්, file එකේවත් print වෙන්නේ නැති වෙයි. ඔයාලට පුළුවන් log4j2.xml එකේ Root level="debug" කියලා වෙනස් කරලා, DEBUG messages එනවාද බලන්න.

වැදගත් Tips:

  • Environment-specific Configurations: Development environment එකේදී DEBUG level එකේ logs ගන්නවා වගේම, Production environment එකේදී INFO, WARN, ERROR, FATAL වගේ critical logs විතරක් ගන්න පුළුවන් විදිහට වෙන වෙනම configuration files හදන්න.
  • Asynchronous Logging: High-performance applications වලදී Asynchronous logging use කරන්න. මේකෙන් application thread එක block වෙන්නේ නැතුව, වෙනම thread එකකින් logging process එක සිද්ධ වෙනවා.
  • Parameterized Logging: String concatenation වලට වඩා logger.info("User {} logged in successfully.", userId); වගේ parameterized logging use කරන්න. මේක performance වලටත්, security වලටත් හොඳයි.
  • Log Analysis Tools: ELK Stack (Elasticsearch, Logstash, Kibana) වගේ tools use කරලා logs analyze කරන්න පුරුදු වෙන්න. මේකෙන් large scale applications වල logs manage කරන එක ගොඩක් ලේසි වෙනවා.

නිගමනය (Conclusion)

හරි යාළුවනේ, ඔන්න අද අපි Java applications වල logging වලට නැතුවම බැරි Apache Log4j framework එක ගැන ගොඩක් දේවල් කතා කළා. Log4j කියන්නේ අපේ applications වල ක්‍රියාකාරිත්වය නිරීක්ෂණය කරන්න, errors හඳුනා ගන්න, සහ performance analyze කරන්න පුළුවන් ඉතාමත් ප්‍රබල tool එකක්. System.out.println() වලට වඩා කොච්චර වාසි තියෙනවද කියලා දැන් ඔයාලට තේරෙන්න ඇති.

දැන් ඔයාලට පුළුවන් මේක ඔයාලගේ project එකකට add කරලා පොඩි experiment එකක් කරලා බලන්න. Log levels වෙනස් කරලා, appenders වෙනස් කරලා, output එක බලන්න. ඒක තමයි මේ වගේ දේවල් ඉගෙන ගන්න හොඳම ක්‍රමය.

කොහොමද යාළුවනේ අද ලිපිය? ඔයාලගේ අදහස් පහලින් comment එකක් දාලා යන්න අමතක කරන්න එපා. Log4j ගැන වෙන මොනවා හරි දැනගන්න ඕනේ නම්, එහෙමත් නැත්තම් ඔයාලගේ අත්දැකීම් මොනවා හරි තියෙනවා නම් ඒවත් share කරන්න. මේ වගේ තවත් technical articles එක්ක ඉක්මනින්ම හම්බ වෙමු. හැමෝටම ජය!