Skip to content

Update HashLog.java ➕ #1

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
141 changes: 33 additions & 108 deletions src/main/java/fr/hashtek/hashlogger/HashLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import java.text.SimpleDateFormat;
import java.util.Date;

public class HashLog
{
public class HashLog {

private final Date createdAt;
private final HashLoggable instance;
Expand All @@ -13,43 +12,11 @@ public class HashLog
private final String log;
private final Exception exception;


/**
* Creates a new instance of HashLog
* (without exception)
*
* @param instance HashLogger instance
* @param author Log author
* @param logLevel Log's level
* @param log Message to output
*/
public HashLog(
HashLoggable instance,
HashLoggable author,
LogLevel logLevel,
String log
)
{
public HashLog(HashLoggable instance, HashLoggable author, LogLevel logLevel, String log) {
this(instance, author, logLevel, log, null);
}

/**
* Creates a new instance of HashLog.
*
* @param instance HashLogger instance
* @param author Log author
* @param logLevel Log's level
* @param log Message to output
* @param exception Exception
*/
public HashLog(
HashLoggable instance,
HashLoggable author,
LogLevel logLevel,
String log,
Exception exception
)
{
public HashLog(HashLoggable instance, HashLoggable author, LogLevel logLevel, String log, Exception exception) {
this.createdAt = new Date();
this.instance = instance;
this.author = author;
Expand All @@ -58,99 +25,57 @@ public HashLog(
this.exception = exception;
}


/**
* Creates a formatted string to output to the console.
*
* @param settings Logger's settings
* @return Formatted string
*/
private String createLog(HashLoggerSettings settings)
{
String date = "";

String logName = settings.doesDisplayShortly()
? this.logLevel.getShortName()
: this.logLevel.getFullName();

String exceptionMessage = "";
private String createLog(HashLoggerSettings settings) {
StringBuilder logBuilder = new StringBuilder();
String logName = settings.doesDisplayShortly() ? logLevel.getShortName() : logLevel.getFullName();

if (settings.doesShowTimestamp()) {
date = new SimpleDateFormat(" (MM-dd-yy HH:mm:ss.SSS)").format(this.createdAt);
String timestamp = new SimpleDateFormat(" (MM-dd-yy HH:mm:ss.SSS)").format(createdAt);
logBuilder.append(timestamp);
}

if (this.exception != null) {
exceptionMessage = "\n" + this.exception.getMessage();
logBuilder.append(String.format("[%s: %s.java] %s<%s>%s ",
instance.getClass().getSimpleName(),
author.getClass().getSimpleName(),
logLevel.getColor(),
logName,
LogLevel.INFO.getColor()));

logBuilder.append(log);

if (exception != null) {
logBuilder.append("\n").append(exception.getMessage());
}

return String.format(
"[%s: %s.java]%s %s<%s>%s %s%s",
this.instance.getClass().getSimpleName(),
this.author.getClass().getSimpleName(),
date,
this.logLevel.getColor(),
logName,
LogLevel.INFO.getColor(),
this.log,
exceptionMessage
);
return logBuilder.toString();
}

/**
* Logs itself to the console, according to HashLogger's settings.
*
* @param settings HashLogger's settings
*/
public void log(HashLoggerSettings settings)
{
String output = this.createLog(settings);

if (this.getLogLevel().isInSysErr()) {
public void log(HashLoggerSettings settings) {
String output = createLog(settings);
if (logLevel.isInSysErr()) {
System.err.println(output);
} else {
System.out.println(output);
}
}


/**
* @return Log's creation date.
*/
public Date getCreatedAt()
{
return this.createdAt;
public Date getCreatedAt() {
return new Date(createdAt.getTime());
}

/**
* @return Log's author
*/
public HashLoggable getInstance()
{
return this.instance;
public HashLoggable getInstance() {
return instance;
}

/**
* @return Log's author
*/
public HashLoggable getAuthor()
{
return this.author;
public HashLoggable getAuthor() {
return author;
}

/**
* @return Log's level
*/
public LogLevel getLogLevel()
{
return this.logLevel;
public LogLevel getLogLevel() {
return logLevel;
}

/**
* @return Log's message
*/
public String getLog()
{
return this.log;
public String getLog() {
return log;
}

}