# Logging

## Getting started

Aurelia comes with a flexible and powerful logging system that allows you to display debug and error messages in your applications in a slightly better way than using native `console.log` statements.

Reasons to use logging inside of your apps and plugins include helpful debug messages for other developers (living comments) or displaying helpful information to the end-user in the console when something goes wrong.

The logger is injected using dependency injection into your components:

```typescript
import { ILogger, resolve } from 'aurelia';

export class MyComponent {
  private readonly logger: ILogger = resolve(ILogger).scopeTo('MyComponent');
}
```

In this example, we scope our logger to our component. But scoping is optional, and the logger can be used without using `scopeTo`however, we highly recommend using the scoping feature to group your messages in the console.

## Logging methods

Just like `console.log` the Aurelia logger supports the following methods:

* debug
* info
* warn
* trace

These methods are called on the logger instance you injected into your component.

```typescript
import { ILogger, resolve } from 'aurelia';

export class MyComponent {
  private readonly logger: ILogger = resolve(ILogger).scopeTo('MyComponent');

  public add() {
      this.logger.debug(`Adding something`);
  }
}
```

Just like `console.log` you can also pass in values such as strings, booleans, arrays and objects.

```typescript
import { ILogger, resolve } from 'aurelia';

export class MyComponent {
  private readonly logger: ILogger = resolve(ILogger).scopeTo('MyComponent');

  public add() {
      this.logger.debug(`Adding something`, [
          { prop: 'value', something: 'else' }
      ]);
  }
}
```

## Creating a logger

To create a custom logger for your applications and plugins, you can create a more reusable wrapper around the logging APIs to use in your applications.

```typescript
import { DI, ILogger, ConsoleSink, IPlatform, LogLevel, LoggerConfiguration, Registration } from '@aurelia/kernel';
import { BrowserPlatform } from '@aurelia/platform-browser';

const PLATFORM = BrowserPlatform.getOrCreate(globalThis);

const staticContainer = DI.createContainer();
staticContainer.register(Registration.instance(IPlatform, Registration));
staticContainer.register(LoggerConfiguration.create({ sinks: [ConsoleSink], level: LogLevel.fatal }));

export const log = staticContainer.get(ILogger).scopeTo('My App');

```

It might look like a lot of code, but this logger implementation will create a scoped logger wrapper you can import into your applications and use in the following way:

```typescript
log.debug(`Debug message`);
log.warn(`This is a warning`);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.aurelia.io/getting-to-know-aurelia/services-and-runtime-hooks/logging.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
