Bird
Raised Fist0
Selenium Javatesting~20 mins

Logging with Log4j in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Log4j2 Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Log4j Basic Configuration Output
Given this Log4j2 configuration snippet, what will be the output when the logger logs an info message?

<Configuration status="WARN">
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss} %-5level %logger{36} - %msg%n"/>
    </Console>
  </Appenders>
  <Loggers>
    <Root level="info">
      <AppenderRef ref="Console"/>
    </Root>
  </Loggers>
</Configuration>
Selenium Java
Logger logger = LogManager.getLogger("MyLogger");
logger.info("Test message");
AA console output like: "14:30:15 INFO MyLogger - Test message"
BA console output like: "14:30:15 DEBUG MyLogger - Test message"
CNo output because the level is set to WARN
DAn error occurs because the pattern layout is invalid
Attempts:
2 left
💡 Hint
Check the root logger level and the message level.
❓ Troubleshoot
intermediate
2:00remaining
Log4j2 Logger Not Printing Messages
You have this Java code snippet using Log4j2:

Logger logger = LogManager.getLogger(MyClass.class);
logger.debug("Debug message");

The log file remains empty after running the program. What is the most likely cause?
AThe logger level is set higher than DEBUG, so debug messages are ignored
BThe logger name is incorrect and does not match the configuration
CThe log file path is missing in the configuration
DThe logger instance was not created with LogManager
Attempts:
2 left
💡 Hint
Check the configured log level and the message level.
❓ Configuration
advanced
2:30remaining
Log4j2 Rolling File Appender Configuration
Which Log4j2 XML configuration snippet correctly sets up a rolling file appender that rolls over daily and keeps 7 days of logs?
A
&lt;Appenders&gt;
  &lt;RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%i.log.gz"&gt;
    &lt;PatternLayout pattern="%d %p %c{1.} [%t] %m%n"/&gt;
    &lt;Policies&gt;
      &lt;SizeBasedTriggeringPolicy size="10MB"/&gt;
    &lt;/Policies&gt;
    &lt;DefaultRolloverStrategy max="7"/&gt;
  &lt;/RollingFile&gt;
&lt;/Appenders&gt;
B
&lt;Appenders&gt;
  &lt;RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{yyyy-MM-dd}.log.gz"&gt;
    &lt;PatternLayout pattern="%d %p %c{1.} [%t] %m%n"/&gt;
    &lt;Policies&gt;
      &lt;TimeBasedTriggeringPolicy interval="1"/&gt;
    &lt;/Policies&gt;
    &lt;DefaultRolloverStrategy max="7"/&gt;
  &lt;/RollingFile&gt;
&lt;/Appenders&gt;
C
&lt;Appenders&gt;
  &lt;File name="File" fileName="logs/app.log"&gt;
    &lt;PatternLayout pattern="%d %p %c{1.} [%t] %m%n"/&gt;
  &lt;/File&gt;
&lt;/Appenders&gt;
D
&lt;Appenders&gt;
  &lt;RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{MM-dd-yyyy}.log"&gt;
    &lt;PatternLayout pattern="%d %p %c{1.} [%t] %m%n"/&gt;
    &lt;Policies&gt;
      &lt;TimeBasedTriggeringPolicy interval="7"/&gt;
    &lt;/Policies&gt;
    &lt;DefaultRolloverStrategy max="30"/&gt;
  &lt;/RollingFile&gt;
&lt;/Appenders&gt;
Attempts:
2 left
💡 Hint
Look for daily rollover and max 7 files retention.
🔀 Workflow
advanced
2:00remaining
Integrating Log4j2 with Selenium Tests
You want to add Log4j2 logging to your Selenium Java tests to log browser actions. Which step is NOT required in the integration workflow?
AAdd Log4j2 dependencies to your project build file (e.g., pom.xml or build.gradle)
BCreate a Log4j2 configuration file (log4j2.xml) in the resources directory
CReplace all Selenium WebDriver calls with Log4j2 API calls
DCreate a Logger instance in your test classes and add logging statements
Attempts:
2 left
💡 Hint
Consider what logging does versus what Selenium does.
✅ Best Practice
expert
3:00remaining
Best Practice for Sensitive Data in Log4j2 Logs
Which is the best practice to avoid logging sensitive data (like passwords) in Log4j2 logs during automated tests?
AStore sensitive data in plain text in logs but encrypt the log files on disk
BLog all data and delete log files manually after tests
CDisable logging completely during tests to avoid sensitive data exposure
DUse a custom log filter to mask or exclude sensitive data before logging
Attempts:
2 left
💡 Hint
Think about controlling what gets logged rather than deleting logs later.

Practice

(1/5)
1. What is the main purpose of using Log4j in Selenium Java tests?
easy
A. To create graphical user interfaces for tests
B. To speed up the execution of Selenium tests
C. To record messages about test execution for debugging and analysis
D. To manage database connections during tests

Solution

  1. Step 1: Understand Log4j's role

    Log4j is a logging tool used to record messages during program execution.
  2. Step 2: Relate to Selenium tests

    In Selenium tests, logging helps track what happens and find issues.
  3. Final Answer:

    To record messages about test execution for debugging and analysis -> Option C
  4. Quick Check:

    Logging = record messages [OK]
Hint: Log4j records messages, not speeds or creates UI [OK]
Common Mistakes:
  • Thinking Log4j speeds up tests
  • Confusing logging with UI creation
  • Assuming Log4j manages databases
2. Which of the following is the correct way to create a Logger instance using Log4j in Java?
easy
A. Logger logger = Logger.getLogger("MyLogger");
B. Logger logger = new Logger();
C. Logger logger = Logger.createLogger();
D. Logger logger = Logger.initLogger("MyLogger");

Solution

  1. Step 1: Recall Log4j Logger creation syntax

    Log4j uses Logger.getLogger(String name) to create or get a logger.
  2. Step 2: Check options

    Only Logger logger = Logger.getLogger("MyLogger"); uses the correct method Logger.getLogger("MyLogger").
  3. Final Answer:

    Logger logger = Logger.getLogger("MyLogger"); -> Option A
  4. Quick Check:

    Use getLogger() to create Logger [OK]
Hint: Use Logger.getLogger("name") to create logger [OK]
Common Mistakes:
  • Trying to instantiate Logger with new
  • Using non-existent methods like createLogger or initLogger
  • Missing the logger name string
3. Given the following Log4j code snippet, what will be the output if the log level is set to WARN?
Logger logger = Logger.getLogger("TestLogger");
logger.info("Info message");
logger.warn("Warning message");
logger.error("Error message");
medium
A. Info message, Warning message, Error message
B. Warning message, Error message
C. Error message only
D. No output

Solution

  1. Step 1: Understand log level filtering

    Log4j outputs messages at or above the set level. WARN level shows WARN and ERROR but not INFO.
  2. Step 2: Apply to given messages

    INFO is below WARN, so skipped. WARN and ERROR messages are shown.
  3. Final Answer:

    Warning message, Error message -> Option B
  4. Quick Check:

    Log level WARN shows WARN and ERROR [OK]
Hint: Log4j shows messages at or above set level [OK]
Common Mistakes:
  • Assuming all messages print regardless of level
  • Thinking INFO is shown at WARN level
  • Confusing ERROR with higher or lower level
4. You wrote this Log4j code but no logs appear when running your Selenium tests:
Logger logger = Logger.getLogger("MyTestLogger");
logger.debug("Debug message");
What is the most likely reason?
medium
A. The log level is set higher than DEBUG, so debug messages are ignored
B. The logger was not created correctly
C. You must call logger.start() before logging
D. Log4j does not support DEBUG level

Solution

  1. Step 1: Check logger creation

    The logger creation syntax is correct, so this is not the issue.
  2. Step 2: Understand log level filtering

    If the log level is set above DEBUG (like INFO or WARN), debug messages won't show.
  3. Final Answer:

    The log level is set higher than DEBUG, so debug messages are ignored -> Option A
  4. Quick Check:

    Higher log level hides lower level messages [OK]
Hint: Check log level; DEBUG hidden if level is INFO or higher [OK]
Common Mistakes:
  • Thinking logger needs start() method
  • Assuming DEBUG is unsupported
  • Believing logger creation failed silently
5. You want to log messages in your Selenium Java tests using Log4j so that only ERROR and FATAL messages appear in the console, but INFO and WARN messages are saved to a file. Which Log4j feature should you use to achieve this?
hard
A. Use only one appender and filter messages in code before logging
B. Set the global log level to ERROR and log INFO messages manually to file
C. Log4j cannot separate messages by level to different outputs
D. Use two appenders with different thresholds and configure them in log4j.properties

Solution

  1. Step 1: Understand Log4j appenders

    Appenders send log messages to different outputs like console or files.
  2. Step 2: Use multiple appenders with thresholds

    You can configure one appender for console with ERROR level, and another for file with INFO or WARN level.
  3. Step 3: Configure in log4j.properties

    This setup is done in the configuration file, not by manual code filtering.
  4. Final Answer:

    Use two appenders with different thresholds and configure them in log4j.properties -> Option D
  5. Quick Check:

    Multiple appenders + thresholds = selective logging [OK]
Hint: Use multiple appenders with level filters in config file [OK]
Common Mistakes:
  • Trying to filter messages manually in code
  • Setting global level only without appenders
  • Thinking Log4j can't split outputs by level
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard