Skip to content

Commit

Permalink
[pinpoint-apm#10632] Backport: Set the log level of ModuleBoot using …
Browse files Browse the repository at this point in the history
…system environment variables
  • Loading branch information
jaehong-kim committed Jan 22, 2024
1 parent a1f3a91 commit 5cb043b
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2023 NAVER Corp.
*
* Licensed 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 com.navercorp.pinpoint.bootstrap.java9.module;

/**
* @author yjqg6666
*/
enum ModuleLogLevel {

ERROR(40),
WARN(30),
INFO(20),
DEBUG(10),
TRACE(0);

private final int value;

ModuleLogLevel(int value) {
this.value = value;
}

public static ModuleLogLevel of(String label) {
if (label == null) {
return null;
}
try {
return ModuleLogLevel.valueOf(label);
} catch (IllegalArgumentException e) {
return null;
}
}

public static ModuleLogLevel of(int value) {
switch (value) {
case 40:
return ERROR;
case 30:
return WARN;
case 20:
return INFO;
case 10:
return DEBUG;
case 0:
return TRACE;
}
return null;
}

public boolean logTrace() {
return checkLevel(TRACE);
}

public boolean logDebug() {
return checkLevel(DEBUG);
}

public boolean logInfo() {
return checkLevel(INFO);
}

public boolean logWarn() {
return checkLevel(WARN);
}

public boolean logError() {
return checkLevel(ERROR);
}

private boolean checkLevel(ModuleLogLevel check) {
return check.value >= this.value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,43 @@

package com.navercorp.pinpoint.bootstrap.java9.module;


/**
* @author Woonduk Kang(emeroad)
*/
public final class ModuleLogger {

private static final ModuleLogLevel LOG_LEVEL = getLogLevel();
private final String loggerName;
private final int prefixSize;

public static ModuleLogger getLogger(String loggerName) {
return new ModuleLogger(loggerName);
}

private static ModuleLogLevel getLogLevel() {
String logLevel = System.getProperty("pinpoint.agent.bootlogger.loglevel", ModuleLogLevel.INFO.name());
logLevel = logLevel.toUpperCase();

final ModuleLogLevel level = ModuleLogLevel.of(logLevel);

return level != null ? level : ModuleLogLevel.INFO;
}

private ModuleLogger(String loggerName) {
this.loggerName = loggerName;
this.prefixSize = getLength(loggerName) + 3;
}

public void info(String log) {
StringBuilder sb = new StringBuilder(getLength(log) + prefixSize);
sb.append('[');
sb.append(loggerName);
sb.append("] ");
sb.append(log);
System.out.println(sb.toString());
if (LOG_LEVEL.logInfo()) {
StringBuilder sb = new StringBuilder(getLength(log) + prefixSize);
sb.append('[');
sb.append(loggerName);
sb.append("] ");
sb.append(log);
System.out.println(sb.toString());
}
}

private int getLength(String log) {
Expand All @@ -48,5 +61,4 @@ private int getLength(String log) {
}
return log.length();
}

}

0 comments on commit 5cb043b

Please sign in to comment.