Skip to content

Commit

Permalink
Merge pull request #2 from r4isen1920/main
Browse files Browse the repository at this point in the history
Add `setTagsOutputVisibility` option to Logging
  • Loading branch information
stirante authored Sep 24, 2024
2 parents e09f926 + fcdafa0 commit 8a84be0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
1 change: 1 addition & 0 deletions docs/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ The `init` method initializes the logging system, setting up commands and defaul
- **setLevel**: Sets the global logging level. By default, info and higher levels are enabled.
- **setFilter**: Sets a filter to control which loggers are active. By default, all loggers are active.
- **setFormatFunction**: Customizes how log messages are formatted.
- **setTagsOutputVisibility**: When true, the tags of the logger will be appended next to its name. By default, this is disabled.
- **setJsonFormatter**: Sets a JSON formatter for stringifying objects and arrays. By default, `ColorJSON.DEFAULT` is used.

### Logging Methods
Expand Down
29 changes: 20 additions & 9 deletions src/Logging.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-unused-labels */
import { system, world } from "@minecraft/server"
import ChatColor from "./ChatColor";
import ColorJSON from "./ColorJSON";
Expand Down Expand Up @@ -122,9 +123,10 @@ function starMatch(pattern: string, str: string): boolean {
}

type LoggingSettings = {
filter: string[],
filter: '*' | string[],
level: LogLevel,
formatFunction: (level: LogLevel, logger: Logger, message: string) => string
outputTags: boolean,
formatFunction: (level: LogLevel, logger: Logger, message: string, tags?: string[] | undefined) => string
messagesJoinFunction: (messages: string[]) => string
jsonFormatter: ColorJSON,
outputConfig: OutputConfig
Expand All @@ -133,8 +135,10 @@ type LoggingSettings = {
const loggingSettings: LoggingSettings = {
level: LogLevel.Info,
filter: ['*'],
formatFunction: (level: LogLevel, logger: Logger, message: string) => {
return `[${level}][${ChatColor.MATERIAL_EMERALD}${logger.name}${ChatColor.RESET}] ${message}`
outputTags: false,
formatFunction: (level: LogLevel, logger: Logger, message: string, tags=undefined) => {
const _tags = tags !== undefined ? `§7${tags.map(tag => `[${tag}]`).join('')}§r` : '';
return `[${level}][${ChatColor.MATERIAL_EMERALD}${logger.name}${ChatColor.RESET}]${_tags} ${message}`
},
messagesJoinFunction: (messages: string[]) => {
return messages.join(' ')
Expand Down Expand Up @@ -195,9 +199,9 @@ export class Logger {
}
/**
* Filter the loggers by the given tags. Tags can use the `*` wildcard.
* @param {array} filter - The filter to set.
* @param {'*' | string[]} filter - The filter to set.
*/
static setFilter(filter: string[]) {
static setFilter(filter: '*' | string[]) {
loggingSettings.filter = filter;
}
/**
Expand All @@ -214,6 +218,13 @@ export class Logger {
static setMessagesJoinFunction(func: (messages: string[]) => string) {
loggingSettings.messagesJoinFunction = func;
}
/**
* Set the tag visibility for the logger. When true, tags will be printed in the log. Disabled by default.
* @param visible
*/
static setTagsOutputVisibility(visible: boolean) {
loggingSettings.outputTags = visible;
}
/**
* Set the JSON formatter for the logger.
* @param {ColorJSON} formatter - The json formatter to set.
Expand All @@ -232,7 +243,7 @@ export class Logger {
* Returns a new Logger.
*
* @param {string} name - The name of the Logger.
* @param {array} tags - The tags for the Logger as strings.
* @param {string[]} tags - The tags for the Logger as strings.
*
* @returns {Logger} A new Logger.
*/
Expand All @@ -248,7 +259,7 @@ export class Logger {
* Construct a new Logger
*
* @param {string} name - The name of the Logger.
* @param {array} tags - The tags for the logger as strings.
* @param {string[]} tags - The tags for the logger as strings.
*/
private constructor(public name: string, public tags: string[] = []) {
}
Expand Down Expand Up @@ -298,7 +309,7 @@ export class Logger {
}
return x.toString() + ChatColor.RESET;
});
const formatted = loggingSettings.formatFunction(level, this, loggingSettings.messagesJoinFunction(msgs));
const formatted = loggingSettings.formatFunction(level, this, loggingSettings.messagesJoinFunction(msgs), loggingSettings.outputTags ? this.tags : undefined);
const outputs = loggingSettings.outputConfig[level.level] || [OutputType.Chat, OutputType.ConsoleInfo];
if (outputs.includes(OutputType.Chat)) {
world.sendMessage(formatted);
Expand Down

0 comments on commit 8a84be0

Please sign in to comment.