Skip to content

Commit

Permalink
Merge pull request #63 from arshad-yaseen/improve-logger
Browse files Browse the repository at this point in the history
Improved Logger
  • Loading branch information
arshad-yaseen authored Oct 24, 2024
2 parents 2c6a2ff + cc71782 commit 7fac6bf
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 72 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ There are other providers and models available. Here is a list:
| --------- | ------------------------------------------------------------------------- |
| Groq | `llama-3-70b` |
| OpenAI | `gpt-4o`, `gpt-4o-mini`, `o1-preview`, `o1-mini` |
| Anthropic | `claude-3-5-Sonnet`, `claude-3-opus`, `claude-3-sonnet`, `claude-3-haiku` |
| Anthropic | `claude-3-5-sonnet`, `claude-3-opus`, `claude-3-sonnet`, `claude-3-haiku` |

### Custom Model

Expand Down
6 changes: 3 additions & 3 deletions src/classes/copilot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
getCopilotProviderEndpoint,
parseProviderChatCompletion,
} from '../helpers/provider';
import {logger} from '../logger';
import {deprecated, report} from '../logger';
import generateCompletionPrompt from '../prompts/completion-prompt';
import {
ChatCompletion,
Expand Down Expand Up @@ -145,7 +145,7 @@ export class Copilot {
if (typeof this.model === 'object' && 'transformResponse' in this.model) {
const transformedResponse = this.model.transformResponse(chatCompletion);
if ('completion' in transformedResponse) {
logger.warn(
deprecated(
'The `completion` property in `transformResponse` function is deprecated. Please use `text` instead.',
);
}
Expand All @@ -163,7 +163,7 @@ export class Copilot {
}

private handleCompletionError(error: unknown): CompletionResponse {
const errorDetails = logger.logError(error);
const errorDetails = report(error);

return {error: errorDetails.message, completion: null};
}
Expand Down
10 changes: 5 additions & 5 deletions src/core/completion/handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {CompletionValidator} from '../../classes';
import {CompletionCache} from '../../classes/completion-cache';
import {constructCompletionMetadata, fetchCompletionItem} from '../../helpers';
import {logger} from '../../logger';
import {report} from '../../logger';
import {
CompletionMetadata,
EditorInlineCompletionsResult,
Expand Down Expand Up @@ -143,11 +143,11 @@ const handleInlineCompletions = async ({
{insertText: formattedCompletion, range: completionInsertionRange},
]);
}
} catch (err) {
} catch (error) {
if (onError) {
onError(err as Error);
} else if (!isCancellationError(err)) {
logger.logError(err);
onError(error as Error);
} else if (!isCancellationError(error)) {
report(error);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/core/completion/register.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {logger} from '../../logger';
import {deprecated, report, warn} from '../../logger';
import {
CompletionRegistration,
Disposable,
Expand Down Expand Up @@ -126,7 +126,7 @@ export const registerCompletion = (
if (options.onError) {
options.onError(error as Error);
} else {
logger.logError(error);
report(error);
}

return {
Expand All @@ -149,7 +149,7 @@ export const registerCompletion = (
const handleTriggerCompletion = (editor: StandaloneCodeEditor) => {
const state = editorCompletionState.get(editor);
if (!state) {
logger.warn(
warn(
'Completion is not registered. Use `registerCompletion` to register completion first.',
);
return;
Expand All @@ -164,7 +164,7 @@ const handleTriggerCompletion = (editor: StandaloneCodeEditor) => {
export const registerCopilot = (
...args: Parameters<typeof registerCompletion>
) => {
logger.warn(
deprecated(
'The `registerCopilot` function is deprecated. Use `registerCompletion` instead.',
);

Expand Down
80 changes: 34 additions & 46 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,41 @@
/**
* Logger class for consistent logging across the application.
*/
class Logger {
private static readonly instance: Logger = new Logger();
private static readonly RED = '\x1b[31m';
private static readonly YELLOW = '\x1b[33m';
private static readonly RESET = '\x1b[0m';
private static readonly BOLD = '\x1b[1m';

private constructor() {}

public static getInstance(): Logger {
return Logger.instance;
const RED = '\x1b[91m';
const YELLOW = '\x1b[93m';
const RESET = '\x1b[0m';
const BOLD = '\x1b[1m';

export const report = (error: unknown): {message: string; stack?: string} => {
let errorMessage: string;
let errorStack: string | undefined;

if (error instanceof Error) {
errorMessage = error.message;
errorStack = error.stack;
} else if (typeof error === 'string') {
errorMessage = error;
} else {
errorMessage = 'An unknown error occurred';
}

public logError(error: unknown): {message: string; stack?: string} {
let errorMessage: string;
let errorStack: string | undefined;

if (error instanceof Error) {
errorMessage = error.message;
errorStack = error.stack;
} else if (typeof error === 'string') {
errorMessage = error;
} else {
errorMessage = 'An unknown error occurred';
}

const formattedError = `${Logger.RED}${Logger.BOLD}[MONACOPILOT ERROR] ${errorMessage}${Logger.RESET}`;
const formattedError = `${RED}${BOLD}[MONACOPILOT ERROR] ${errorMessage}${RESET}`;
if (errorStack) {
console.error(
`${formattedError}\n${RED}Stack trace:${RESET}\n${errorStack}`,
);
} else {
console.error(formattedError);

if (errorStack) {
console.error(
`${Logger.RED}[MONACOPILOT ERROR] Stack trace:${Logger.RESET}\n${errorStack}`,
);
}

return {message: errorMessage, stack: errorStack};
}

public warn(message: string): void {
console.warn(
`${Logger.YELLOW}${Logger.BOLD}[MONACOPILOT WARN] ${message}${Logger.RESET}`,
);
}
return {message: errorMessage, stack: errorStack};
};

public log(message: string): void {
console.log(`${Logger.BOLD}[MONACOPILOT] ${message}${Logger.RESET}`);
}
}
export const deprecated = (message: string): void => {
console.warn(`${YELLOW}${BOLD}[MONACOPILOT DEPRECATED] ${message}${RESET}`);
};

export const warn = (message: string): void => {
console.warn(`${YELLOW}${BOLD}[MONACOPILOT WARN] ${message}${RESET}`);
};

export const logger = Logger.getInstance();
export const log = (message: string): void => {
console.log(`${BOLD}[MONACOPILOT] ${message}${RESET}`);
};
25 changes: 12 additions & 13 deletions tests/ui/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,21 @@ export default function Home() {
const completion = registerCompletion(monaco, editor, {
endpoint: '/api/complete',
language: 'javascript',
trigger: 'onDemand',
maxContextLines: 60,
});

monaco.editor.addEditorAction({
id: 'complete-code',
label: 'Complete Code',
contextMenuGroupId: 'navigation',
contextMenuOrder: 1,
keybindings: [
monaco.KeyMod.CtrlCmd | monaco.KeyMod.Shift | monaco.KeyCode.Space,
],
run: () => {
completion.trigger();
},
});
// monaco.editor.addEditorAction({
// id: 'complete-code',
// label: 'Complete Code',
// contextMenuGroupId: 'navigation',
// contextMenuOrder: 1,
// keybindings: [
// monaco.KeyMod.CtrlCmd | monaco.KeyMod.Shift | monaco.KeyCode.Space,
// ],
// run: () => {
// completion.trigger();
// },
// });

return () => {
completion.deregister();
Expand Down

0 comments on commit 7fac6bf

Please sign in to comment.