Skip to content

Commit

Permalink
GH-548 Replace complex Messenger with simple logging API (Resolve #540)
Browse files Browse the repository at this point in the history
  • Loading branch information
dzikoysk committed Aug 17, 2020
1 parent 6f49781 commit 7638003
Show file tree
Hide file tree
Showing 43 changed files with 132 additions and 2,073 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2020 Dzikoysk
*
* 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 org.panda_lang.language.interpreter.logging;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collection;

import static org.panda_lang.utilities.commons.collection.Lists.add;

public class Channel implements Comparable<Channel> {

private static final Collection<Channel> VALUES = new ArrayList<>();

public static Channel FATAL = add(VALUES, new Channel("fatal", 6.0));
public static Channel ERROR = add(VALUES, new Channel("error", 5.0));
public static Channel WARN = add(VALUES, new Channel("warn", 4.0));
public static Channel INFO = add(VALUES, new Channel("info", 3.0));
public static Channel DEBUG = add(VALUES, new Channel("debug", 2.0));
public static Channel TRACE = add(VALUES, new Channel("trace", 1.0));

private final String channel;
private final double priority;

public Channel(String channel, double priority) {
this.channel = channel;
this.priority = priority;
}

@Override
public int compareTo(@NotNull Channel o) {
return Double.compare(priority, o.priority);
}

public double getPriority() {
return priority;
}

public String getChannel() {
return channel;
}

public static Channel of(String channel) {
for (Channel value : VALUES) {
if (value.getChannel().equalsIgnoreCase(channel)) {
return value;
}
}

throw new UnsupportedOperationException(channel + " is not supported by default");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2020 Dzikoysk
*
* 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 org.panda_lang.language.interpreter.logging;

public interface Logger {

void log(Channel channel, String message);

void exception(Throwable throwable);

default void fatal(String message) {
log(Channel.FATAL, message);
}

default void error(String message) {
log(Channel.ERROR, message);
}

default void warn(String message) {
log(Channel.WARN, message);
}

default void info(String message) {
log(Channel.INFO, message);
}

default void debug(String message) {
log(Channel.DEBUG, message);
}

default void trace(String message) {
log(Channel.TRACE, message);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
* limitations under the License.
*/

package org.panda_lang.language.interpreter.messenger;

import org.slf4j.Logger;
package org.panda_lang.language.interpreter.logging;

/**
* Logger container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,30 @@
* limitations under the License.
*/

package org.panda_lang.language.interpreter.messenger;
package org.panda_lang.language.interpreter.logging;

import org.slf4j.event.Level;
public final class SystemLogger implements Logger {

public final class PandaMessengerMessage implements MessengerMessage {
private final Channel threshold;

private final Level level;
private final String message;
public SystemLogger() {
this(Channel.INFO);
}

public PandaMessengerMessage(Level level, String message) {
this.message = message;
this.level = level;
public SystemLogger(Channel threshold) {
this.threshold = threshold;
}

@Override
public String getContent() {
return message;
public void log(Channel channel, String message) {
if (channel.getPriority() >= threshold.getPriority()) {
System.out.println(message);
}
}

@Override
public Level getLevel() {
return level;
public void exception(Throwable throwable) {
throwable.printStackTrace(System.err);
}

}

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 7638003

Please sign in to comment.