-
Notifications
You must be signed in to change notification settings - Fork 1
Dev #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dev #5
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| <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> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this module registers a Severity: medium Other Locations
🤖 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 |
There was a problem hiding this comment.
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-log4j2even 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
🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.