Git Product home page Git Product logo

angular-simple-logger's Introduction

angular-simple-logger (nemLogging.nemSimpleLogger)

Join the chat at https://gitter.im/nmccready/angular-simple-logger Dependenciesย  Dependenciesย  Build Status

TLDR, Example

Purpose:

To have simplified log levels where a supporting angular module's log levels are independent of the application.

Dependencies: (What can we make what was once simple/complicated (kinda))

How to decide which file from /dist to include in your project:

  • If using bower, include dist/angular-simple-logger.js. This is a drop-in replacement for browser.js. It is prepacked (all dependencies included). Do NOT use with Browserify or Webpack since it will break any commonJS compiler.

  • If using npm with Browserify or Webpack, include dist/index.js (CommonJS version). Do NOT use with bower.

  • *.light.js - any, will/should work with Browser, Brower and npm (with some love, but why?)

Why is it this way? Well visonmedia debug is complicated. See here . I am open to a better way so please work with me, not against me.

Bottom line: Here is what I recommend. If your using bower with a project that depends on this; bundle your vendor files seperatley via gulp, and main bowerfiles. Browserify, and Webpack can work with both bower and npm at the same time so it is up to you to make your whole app play nice (ie you can ignore stuff and not call require).

Also you can override which version (say point to*.light.js) for webpack and or browserify to use/target against.

If your lucky and all of your projects are in npm then you can eliminate bower and not worry about this.

In the end there are plenty of workarounds using the tools explained above.

Basic use:

angular.module('someApp', ['nemLogging'])
//note this can be any type of injectable angular dependency (factory, service.. etc)
.controller("someController", function ($scope, nemSimpleLogger) {
  nemSimpleLogger.doLog = true; //default is true
  nemSimpleLogger.currentLevel = nemSimpleLogger.LEVELS.debug;//defaults to error only
});  

Create a Custom Independent Loggers

(maybe 3 for one lib)

angular.module('someApp', ['nemLogging'])
//note this can be any type of injectable angular dependency (factory, service.. etc)
.service("apiLogger", function ($scope, nemSimpleLogger) {
  var logger = nemSimpleLogger.spawn();
  logger.currentLevel = logger.LEVELS.warn;
  return logger;
})
.service("businessLogicLogger", function ($scope, nemSimpleLogger) {
  var logger = nemSimpleLogger.spawn();
  logger.currentLevel = logger.LEVELS.error;
  return logger;
})
.service("terseLogger", function ($scope, nemSimpleLogger) {
  var logger = nemSimpleLogger.spawn();
  logger.currentLevel = logger.LEVELS.info;
  return logger;
});

Use your new creations!

angular.module('someApp', ['nemLogging'])
//note this can be any type of injectable angular dependency (factory, service.. etc)
.service("booksApi", function (apiLogger, $http) {
  //do something with your books
  $http.get("/ap/books").then(function(data){
    apiLogger.debug("books have come yay!");
  });
})
.controller("businessCtrl", function ($scope, businessLogicLogger, book) {
  businessLogicLogger.debug("new book");
  var b = new book();
  $scope.books = [b];
})
.controller("appCtrl", function ($rootScope, terseLogger) {
  $rootScope.$on "error", function(){
    terseLogger.error("something happened");
  }
});

Override all of $log (optional decorator)

Optionally (default is off) decorate $log to utilize log levels globally within the app.

Note this logger's currentLevel is debug! Where the order is debug, info, warn, error, log.

angular.module('someApp', ['nemLogging']))
.config(function($provide, nemSimpleLoggerProvider) {
  return $provide.decorator.apply(null, nemSimpleLoggerProvider.decorator);
})
.config(function($provide, nemSimpleLoggerProvider) {
  var logger = $provide.decorator.apply(null, nemSimpleLoggerProvider.decorator);
  //override level at config
  logger.currentLevel = logger.LEVELS.error;
  return logger;
})
.run(function($log){
  //at run time
  //override the default log level globally
  $log.currentLevel = $log.LEVELS.error;
});

Optional Debug Levels via debug

If you choose to use the full version of this library and no the light.

You can add finer grain debug levels via the visionmedia/debug API.

To use:

angular.module('someApp', ['nemLogging']))
//as a provider
.config(function(nemDebugProvider) {
  var debug = nemDebugProvider.debug;
  debug.enable("worker:*");
})
.service('LoggerLevelA', function(nemSimpleLogger) {
  //will have debug, info, warn, error, and log at disposal as before, but now debug is using the visionmedia/debug fn
  return nemSimpleLogger.spawn("worker:a");
})
.service('LoggerLevelB', function(nemSimpleLogger) {
  return nemSimpleLogger.spawn("worker:b");
})
//heck maybe you don't want all of the logger interface only want debug.. then
.service('JustDebugC',function(nemDebug) {
  return nemDebug("worker:c");
})
.run(function(nemDebug){
  //enable another debug level
  nemDebug.enable("coolStuff:*");
});

API

Underneath it all it is still calling $log so calling the logger for logging itself is the same.

  • LEVELS: available are debug, info, warn, error, log

  • doLog (boolean) - deaults to true. If set to false all logging for that logger instance is disabled.

  • currentLevel (number) - defaults to error: 4 corresponds to the current log level provided by LEVELS.

  • spawn - create a independent logger accepts a logger or a string (see visionmedia debug notes above). Defaults to $log

angular-simple-logger's People

Contributors

gitter-badger avatar jessecstewart avatar nmccready avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

angular-simple-logger's Issues

Cannot find module './debug'

Hey,

I'm facing an issue here.
Error: Cannot find module './debug' from '/Users/user/project/ui/node_modules/angular-simple-logger/dist'

I'm using browserify and this project is only frontend.

Thanks in advance,
Celso

angular-simple-logger is polluting the global namespace with local variables

While looking at #13 I also noticed that many local variables in this package are accessible globally.

In the following method both key and val become global variables.

    _isValidLogObject = function(logObject) {
      var isValid;
      isValid = false;
      if (!logObject) {
        return isValid;
      }
      for (key in _fns) {
        val = _fns[key];
        isValid = (logObject[val] != null) && typeof logObject[val] === 'function';
        if (!isValid) {
          break;
        }
      }
      return isValid;
    };

As the default settings for CoffeeScript seems to be to generate local variables (with var statements) and from what I can see you haven't specified any strange options, I have no idea how it ends up like this here.

decorator bug angular-simple-logger.min.js (min only)

Error when using decorator

  }(), this.decorator = ["$log", function(e) {
                var o;
                return o = new n(e), o.currentLevel = r.debug, o

will only work when

  }(), this.decorator = ["$log", function($delegate) {
                var o;
                return o = new n($delegate), o.currentLevel = r.debug, o

Wonder if we can skip this section for minify.

Error: Cannot find module './debug' when using browserify

I get the following error when using angular-simple-logger with browserfy:

Error: Cannot find module './debug' from '/Users/Demo/DemoProject/node_modules/angular-simple-logger/dist'

Changing in package.json the main file to the light version works around this problem:
"main": "./dist/angular-simple-logger.light.js",

Allow params for logging functions

Logging methods should allow passing additional params.

Example:

var logger = nemSimpleLogger.spawn();
logger.error('Something happened', 'Error message');

@$log is invalid thrown if Array is polyfilled

I have noticed that if the Array object is polyfilled with a few extra methods (the ones I use are contains, find and equals) then angular-simple-logger breaks when checking if the $log object is a valid logging object.

This is because the for-in loop that you are using will find those polyfilled methods in addition to the actual array values. In normal JavaScript I'd either use hasOwnProperty() or even simpler and faster, use the numeric index.

I really don't do CoffeeScript so I'll leave the fix to you. But maybe something like this?

_isValidLogObject = (logObject) ->
  if !logObject
    return false
  idx = 0
  while idx < _fns.length
    val = _fns[idx]
    if logObject[val] == null or typeof logObject[val] != 'function'
      return false
    ++idx
  true

As you see I switched the for loop to a while (since CoffeeScript for loops are weird) and removed the isValid variable and replaced it with instant return statements instead.

The second part is optional of course, I just find it cleaner. ๐Ÿ˜„

Bower Hell and visionmedia debug dependency

So since this lib is an important dependency to angular-leaflet-directive and angular-google-maps via bower. It should probably point to the angular-simple-logger.light.js where advanced features via debug are off. That way no Browserify, and Webpack issues should occur when angular-leaflet-directive or ui-gmap are bundled.

This allows a developer to use Webpack, Browserify in conjunction with npm, bower, and main-bower-files point to the specific (maybe full-version) in their application.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.