Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ TODO

The framework is organized into several key modules:

Module | Purpose
----------------------------------|-----------------------------------------------------------------------------------------------------
microsphere-logging-core | Provides the core utilities across various domains like annotations, collections, concurrency, etc.
microsphere-logging-test | Provides the models and components for logging testing.
microsphere-logging-dependencies | Manages dependency versions across the project.
microsphere-logging-parent | Parent POM with shared configurations.
Module | Purpose
----------------------------------|-------------------------------------------------------------------------
microsphere-logging-parent | Parent POM with shared configurations.
microsphere-logging-dependencies | Manages dependency versions across the project.
microsphere-logging-core | Provides the core features for logging.
microsphere-logging-logback | Provides the extensions features for logback.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Modules table doesn’t list microsphere-logging-log4j2 even though this PR adds that module to the build, so the README may be out of sync for users looking for Log4j2 support.

Severity: low

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

microsphere-logging-test | Provides the extensions of JUnit4 or JUnit Jupiter for logging testing.

## Getting Started

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import java.util.List;
import java.util.Set;

import static java.util.Collections.emptyList;
import static java.util.Collections.emptySet;

/**
* Throwable {@link Logging}
*
Expand All @@ -31,12 +34,12 @@ public class ThrowableLogging implements Logging {

@Override
public List<String> getLoggerNames() {
return List.of();
return emptyList();
}

@Override
public Set<String> getSupportedLoggingLevels() {
return Set.of();
return emptySet();
}

@Override
Expand All @@ -46,7 +49,6 @@ public String getLoggerLevel(String loggerName) {

@Override
public void setLoggerLevel(String loggerName, String levelName) {

}

@Override
Expand All @@ -58,4 +60,4 @@ public String getParentLoggerName(String loggerName) {
public String getName() {
throw new RuntimeException("For testing...");
}
}
}
6 changes: 6 additions & 0 deletions microsphere-logging-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
<version>${revision}</version>
</dependency>

<dependency>
<groupId>io.github.microsphere-projects</groupId>
<artifactId>microsphere-logging-log4j2</artifactId>
<version>${revision}</version>
</dependency>

<dependency>
<groupId>io.github.microsphere-projects</groupId>
<artifactId>microsphere-logging-test</artifactId>
Expand Down
71 changes: 71 additions & 0 deletions microsphere-logging-log4j2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>io.github.microsphere-projects</groupId>
<artifactId>microsphere-logging-parent</artifactId>
<version>${revision}</version>
<relativePath>../microsphere-logging-parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<groupId>io.github.microsphere-projects</groupId>
<artifactId>microsphere-logging-log4j2</artifactId>
<version>${revision}</version>
<packaging>jar</packaging>

<name>Microsphere :: Logging :: Logj4</name>
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Maven <name> is Microsphere :: Logging :: Logj4, which looks like a typo and will show up in published artifact metadata.

Severity: low

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

<description>Microsphere Logging Log4j2</description>

<dependencies>

<!-- Microsphere Logging -->
<dependency>
<groupId>io.github.microsphere-projects</groupId>
<artifactId>microsphere-logging-core</artifactId>
<version>${revision}</version>
</dependency>

<!-- Microsphere Java -->
<dependency>
<groupId>io.github.microsphere-projects</groupId>
<artifactId>microsphere-java-annotations</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>io.github.microsphere-projects</groupId>
<artifactId>microsphere-java-core</artifactId>
<optional>true</optional>
</dependency>

<!-- Log4j2 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<optional>true</optional>
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this module registers a Logging provider via SPI, making log4j-api/log4j-core optional can cause ServiceLoader/ServiceConfigurationError at runtime if a consumer includes this module but doesn’t also include Log4j2 on the classpath.

Severity: medium

Other Locations
  • microsphere-logging-log4j2/pom.xml:53

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<optional>true</optional>
</dependency>

<!-- Testing -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.microsphere.logging.log4j2;

import io.microsphere.logging.Logging;
import io.microsphere.logging.log4j2.util.LoggerUtils;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Logger;

import java.util.List;
import java.util.Set;

import static io.microsphere.constants.SymbolConstants.DOT;
import static io.microsphere.logging.DefaultLoggingLevelsResolver.INSTANCE;
import static io.microsphere.logging.log4j2.util.LoggerUtils.getLevelString;
import static io.microsphere.logging.log4j2.util.LoggerUtils.getLoggerContext;
import static java.util.stream.Collectors.toList;
import static org.apache.logging.log4j.LogManager.ROOT_LOGGER_NAME;

/**
* {@link Logging} class based on the log4j2 framework.
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
* @see Logging
* @since 1.0.0
*/
public class Log4j2Logging implements Logging {

@Override
public List<String> getLoggerNames() {
return getLoggerContext()
.getLoggers()
.stream().map(Logger::getName)
.collect(toList());
}

@Override
public Set<String> getSupportedLoggingLevels() {
return INSTANCE.resolve(Level.class);
}

@Override
public String getLoggerLevel(String loggerName) {
return getLevelString(loggerName);
}

@Override
public void setLoggerLevel(String loggerName, String levelName) {
LoggerUtils.setLoggerLevel(loggerName, levelName);
}

@Override
public String getParentLoggerName(String loggerName) {
if (ROOT_LOGGER_NAME.equals(loggerName)) {
return null;
}
int lastDotIndex = loggerName.lastIndexOf(DOT);
if (lastDotIndex == -1) {
return ROOT_LOGGER_NAME;
}
String parentLoggerName = loggerName.substring(0, lastDotIndex);
return parentLoggerName;
}

@Override
public String getName() {
return "Log4j2";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.microsphere.logging.log4j2.util;


import io.microsphere.annotation.Nonnull;
import io.microsphere.annotation.Nullable;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LoggerContext;

import static org.apache.logging.log4j.Level.toLevel;
import static org.apache.logging.log4j.core.LoggerContext.getContext;

/**
* The Utilities class of Log4j2 Logger
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
* @see Logger
* @since 1.0.0
*/
public abstract class LoggerUtils {

static final LoggerContext loggerContext = getContext();

/**
* Get the LoggerContext
*
* @return LoggerContext , never be null
*/
@Nonnull
public static LoggerContext getLoggerContext() {
return loggerContext;
}

/**
* Get the Logger by specified name
*
* @param loggerName Logger name
* @return Logger , maby be null
*/
@Nullable
public static Logger getLogger(@Nonnull String loggerName) {
return loggerContext.getLogger(loggerName);
}

/**
* Get the Logger by the request class
*
* @param requestClass request class
* @return Logger , maby be null
*/
@Nullable
public static Logger getLogger(@Nonnull Class<?> requestClass) {
return getLogger(requestClass.getName());
}

/**
* Get the Level of Logger by the specified logger name
*
* @param loggerName Logger name
* @return the Level of Logger , may be null
*/
@Nullable
public static Level getLevel(@Nonnull String loggerName) {
Logger logger = getLogger(loggerName);
return getLevel(logger);
}

/**
* Get the Level of Logger
*
* @param logger Logger
* @return the Level of Logger , may be null
*/
@Nullable
public static Level getLevel(@Nullable Logger logger) {
Level level = null;
if (logger != null) {
level = logger.getLevel();
}
return level;
}

/**
* Set the Level of Logger by the specified logger name
*
* @param loggerName Logger name
* @param levelName level string, if it's <code>null</code>, then {@link Level#DEBUG} will be taken.
*/
public static void setLoggerLevel(String loggerName, String levelName) {
Logger logger = getLogger(loggerName);
setLoggerLevel(logger, levelName);
}

/**
* Set the Level of Logger
*
* @param logger Logger
* @param levelString level string, if it's <code>null</code>, then {@link Level#DEBUG} will be taken.
*/
public static void setLoggerLevel(@Nullable Logger logger, @Nullable String levelString) {
setLoggerLevel(logger, toLevel(levelString));
}

/**
* Set the Level of Logger
*
* @param logger Logger
* @param level level
*/
public static void setLoggerLevel(@Nullable Logger logger, @Nonnull Level level) {
if (logger != null) {
logger.setLevel(level);
}
}

/**
* Get the Level String of Logger by the specified logger name
*
* @param loggerName Logger name
* @return the Level String of Logger , may be null
*/
@Nullable
public static String getLevelString(@Nonnull String loggerName) {
Logger logger = getLogger(loggerName);
Level level = getLevel(logger);
return getLevelString(level);
}

/**
* Get the Level String of Logger
*
* @param level Level
* @return the Level String of Logger , may be null
*/
@Nullable
public static String getLevelString(@Nullable Level level) {
return level == null ? null : level.toString();
}

private LoggerUtils() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.microsphere.logging.log4j2.Log4j2Logging
Loading
Loading