npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ionic-logging-service

v18.0.0

Published

Logging functionalities for apps built with Ionic framework

Downloads

2,615

Readme

ionic-logging-service

The dependencies used by the latest version are the same as needed for Ionic 7.0.0. For older versions use:

| ionic-logging-service | Ionic | Angular | ------ | -------- | ------ | 18.0.0 | >= 7.0.0 | >=17.0.0 | 17.0.0 | >= 6.1.9 | ^16.0.0 | 16.0.0 | >= 6.1.9 | ^15.0.0 | 15.0.0 | >= 6.1.9 | ^14.0.0 | 14.0.0 | >= 6.0.0 | ^13.0.0 | 13.0.0 | >= 5.0.0 | ^12.0.0 | 12.0.0 | >= 5.0.0 | ^11.0.0 | 11.0.0 | >= 5.0.0 | ^10.0.0 | 9.0.0 | >= 5.0.0 | ^9.0.0 | 8.0.0 | >= 4.7.0 | ^8.0.0 | 7.0.0 | >= 4.0.0-rc | ^7.0.0 | 6.0.0 | >= 4.0.0-beta | ^6.0.0 | 5.1.0 | >= 3.9.0 | ^5.0.0 | 3.1.0 | >= 3.0.0 | ^4.0.0 | 2.0.0 | >= 2.2.0 | ^2.4.8 | 1.2.1 | >= 2.0.0 | ^2.2.1

This service encapsulates log4javascript's functionalities for apps built with Ionic framework.

For a sample, just have a look at ionic-logging-viewer.

Usage

First, you need to import the LoggingServiceModule in your AppModule. The next step is typically the configuration (see below). And then, finally, you can use the LoggingService in your code, e.g.:

import { Logger, LoggingService } from "ionic-logging-service";

export class MyComponent {

  private logger: Logger;

  constructor(
    loggingService: LoggingService) {

    this.logger = loggingService.getLogger("MyApp.MyComponent");
    const methodName = "ctor";
    this.logger.entry(methodName);

    ...

    this.logger.exit(methodName);
  }

  public myMethod(index: number, message: string): number[] {
    const methodName = "myMethod";
    this.logger.entry(methodName, index, number);

    try {
      ...
    } catch (e) {
      this.logger.error(methodName, "some error", e);
    }

    this.logger.exit(methodName);
    return result;
  }
}

Depending how the code is called, this could produce the following output in the browser's console:

I  18:49:43.794  MyApp.MyComponent  ctor  entry
I  18:49:43.797  MyApp.MyComponent  ctor  exit
I  18:49:43.801  MyApp.MyComponent  myMethod  entry  42  Hello
E  18:49:43.814  MyApp.MyComponent  myMethod  some error
I  18:49:43.801  MyApp.MyComponent  myMethod  exit  [2, 5, 99]

Logger

A logger is the component responsible for logging. Typically, you have one logger per every class. The logger name describe the place where in your app the class is placed. The single parts are separated by dots ('.'). This is quite the same as with namespaces in dotnet or packages in Java.

This builds some kind of hierarchy. E.g., if you have a logger named A.B.C.D, you get automatically also loggers for A.B.C, A.B and A. Additionally, there is the so-called root logger, which is the parent of all other loggers.

The hierarchy is important, since the loggers inherit the log level from there parent - if there is no other level defined. That means, you can define just one log level for the complete app (by setting the root logger's level), and you can par example define, you do not want to see logs written for logger A.B.C (this includes also A.B.C.D).

Level

Every log message has a level. This is the severity of the message. Available levels are TRACE, DEBUG, INFO, WARN, ERROR and FATAL - these correspond to the logging methods trace, debug, info, warn, error and fatal of Logger. Levels are ordered as follows: TRACE < DEBUG < INFO < WARN < ERROR < FATAL. This means the FATAL is the most severe and TRACE the least. Also included are levels called ALL and OFF intended to enable or disable all logging respectively.

Setting a level to a logger disables log messages of severity lower than that level. For instance, if a level of INFO is set on a logger then only log messages of severity INFO or greater will be logged, meaning DEBUG and TRACE messages will not be logged.

Appender

Appenders make the logs visible, e.g. by writing them to the browser's console. This is quite useful during development, either in console or using ionic serve --consolelogs. But later, you will need other logs:

  • AjaxAppender: sends the log messages to a backend server
  • MemoryAppender: keeps the log messages in memory
  • LocalStorageAppender: stores the log messages in local storage

If you want to see a complete example, have a look at ionic-feedback-sample.

Configuration

By default, the following configuration is used:

  • Logger:

    • root: Level.WARN
  • Appender:

    • BrowserConsoleAppender
    • MemoryAppender

To change it, just call configure(). This method takes an object of type LoggingServiceConfiguration.

The recommended way is to place the configuration in environment.ts:

export const environment = {
  logging: {
    ...
  }
};

Call configure() in your app.module.ts:

export function configureLogging(loggingService: LoggingService): () => void {
  return () => loggingService.configure(environment.logging);
}

@NgModule({
  ...
  imports: [
    ...
    LoggingServiceModule
  ],
  providers: [
    {
      deps: [LoggingService],
      multi: true,
      provide: APP_INITIALIZER,
      useFactory: configureLogging
    }
  ]
})
export class AppModule { }

logLevels

logLevels gets an array of log level definitions for different loggers, e.g.

{
  "logLevels": [
    {
      "loggerName": "root",
      "logLevel": "DEBUG"
    },
    {
      "loggerName": "MyApp.MyNamespace.MyLogger",
      "logLevel": "INFO"
    }
  ]
};

That means, instead of the default log level WARN, you want to log all messages with level DEBUG and higher. Only for MyApp.MyNamespace.MyLogger, you want to restrict the level to INFO.

ajaxAppender

With ajaxAppender, you add an additional appender of type AjaxAppender, which sends the log messages to a backend server.

browserConsoleAppender

With browserConsoleAppender, it is possible to configure the BrowserConsoleAppender, which writes the log to the browser's console.

localStorageAppender

With localStorageAppender, you add an additional appender of type LocalStorageAppender, which stores log messages in the local storage.

memoryAppender

With memoryAppender, it is possible to configure the MemoryAppender, which keeps log messages in the memory.

API

see API documentation.