Skip to content
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

Option to pretty print error log #1925

Closed
1 task done
BrunnerLivio opened this issue Jul 11, 2022 · 2 comments
Closed
1 task done

Option to pretty print error log #1925

BrunnerLivio opened this issue Jul 11, 2022 · 2 comments

Comments

@BrunnerLivio
Copy link
Member

BrunnerLivio commented Jul 11, 2022

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

The JSON which is printed during an error message is not really readable.

Describe the solution you'd like

Better readability of the current health check status. Example:

terminus-log

This shall be enabled via:

Terminus.forRoot({ errorLogStyle: 'pretty-emoji' | 'pretty' | 'json' })

This would allow one to switch between different log styles depending on the environment

Terminus.forRoot({ errorLogStyle: process.env.NODE_ENV === 'production' ? 'json' : 'pretty-emoji' })

Teachability, documentation, adoption, migration strategy

The json-option would remain the default in case someone uses a log parser tool (e.g. Splunk) to not break any changes.

The docs should recommend to use the code-snippet above. (json in prod but pretty-emoji locally)

What is the motivation / use case for changing the behavior?

The JSON which is printed during an error message is not really readable.

@BrunnerLivio
Copy link
Member Author

Note to myself:

The following code produces the result above (in health-check.service.ts)

    printIndent (level) {
        if(level === 0) {
            return '';
        }
        return `${' '.repeat(level * 2)}- `;
    }
    
    printIndicatorSummary (result, level = 1) {
        let message = ''
        for(const [key, value] of Object.entries(result)) {
            if(typeof value === 'object' && value !== null) {
                message += `${this.printIndent(level)}${key}:\n${this.printIndicatorSummary(value, level + 1)}\n`;
            } else {
                message += `${this.printIndent(level)}${key}: ${value}\n`
            }
        }
        return message;
    }
    
    printSummary (result){
        let message = '';
    
        for(const [key, value] of Object.entries(result)) {
            if(value.status === "up") {
                message += `\x1b[0m\x1b[32m${key}: ✅\n${this.printIndicatorSummary(value)}\x1b[0m\n`
            }
            if(value.status === "down") {
                message += `\x1b[0m\x1b[31m${key}: ❌\n${this.printIndicatorSummary(value)}\x1b[0m\n`
            }
        }
        return message;
    }

    /**
     * Logs an error message of terminus
     * @param message The log message
     * @param error The error which was thrown
     */
    logError(message, causes) {
        message = `${message}\n\n${this.printSummary(causes)}`;
        this.logger.error(message);
    }

@BrunnerLivio BrunnerLivio changed the title Option to pretty print in log Option to pretty print error log Jul 11, 2022
BrunnerLivio added a commit that referenced this issue Jul 30, 2022
@BrunnerLivio
Copy link
Member Author

Released with 9.1.0 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant