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

Add handling of different log levels for messages from Elm #221

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
10 changes: 7 additions & 3 deletions ts/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ import * as fs from 'fs';
import * as path from 'path';
import Server from '../server/app';
import Analyser from '../analyser';
import { LogLevel } from '../domain';

var args = minimist(process.argv.slice(2), {
alias: {
serve: 's',
help: 'h',
port: 'p',
version: 'v',
open: 'o'
open: 'o',
quiet: 'q',
},
boolean: ['serve', 'help', 'version', 'open'],
boolean: ['serve', 'help', 'version', 'open', 'quiet'],
string: ['port', 'elm-format-path', 'format']
});

Expand All @@ -29,7 +31,8 @@ var args = minimist(process.argv.slice(2), {
port: args.port || 3000,
elmFormatPath: elmFormatPath,
format: validFormats.indexOf(args.format) != -1 ? args.format : 'human',
open: args.open || false
open: args.open || false,
logLevel: args.quiet ? LogLevel.ERROR : LogLevel.INFO
};
const info = {
version: elmAnalyseVersion,
Expand All @@ -50,6 +53,7 @@ var args = minimist(process.argv.slice(2), {
console.log(' --serve, -s Enable server mode. Disabled by default.');
console.log(' --port, -p The port on which the server should listen. Defaults to 3000.');
console.log(' --open, -o Open default browser when server goes live.');
console.log(' --quiet, -q Print fewer log messages.');
console.log(' --elm-format-path Path to elm-format. Defaults to `elm-format`.');
console.log(' --format Output format for CLI. Defaults to "human". Options "human"|"json"');
process.exit(1);
Expand Down
13 changes: 11 additions & 2 deletions ts/domain.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
interface Info {
config: Config;
}

enum LogLevel {
INFO,
WARN,
ERROR,
}

interface Config {
format: string | undefined;
open: boolean;
port: number;
elmFormatPath: string;
logLevel: LogLevel;
}

export interface DependencyPointer {
Expand Down Expand Up @@ -48,7 +56,7 @@ export interface FixedFile {
}

export interface LogMessage {
level: string;
level: keyof typeof LogLevel;
message: string;
}
interface ElmApp {
Expand Down Expand Up @@ -182,5 +190,6 @@ export {
FileContentSha,
EditorElmApp,
EditorMessage,
EditorData
EditorData,
LogLevel
};
6 changes: 4 additions & 2 deletions ts/util/logging-ports.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Config, ElmApp, LogMessage } from '../domain';
import { Config, ElmApp, LogLevel, LogMessage } from '../domain';

export function setup(app: ElmApp, config: Config) {
if (config.format === 'human') {
app.ports.log.subscribe((data: LogMessage) => {
console.log(data.level + ':', data.message);
if (LogLevel[data.level] >= config.logLevel) {
console.log(data.level + ':', data.message);
}
});
}
}