Giter Site home page Giter Site logo

log_calico's Introduction

Log Calico

pub package

Provides log filtering, buffering, and retry.

Inspired by cookpad/Puree-Swift.

Usage

Define your own Filter/Output

Filter

Filter converts Log to List<Log>.

You can increase the log, transform it, or empty it.

class ActionFilter extends Filter {
  ActionFilter({required super.tagPattern});

  @override
  List<Log> transform(Log log) {
    final target = log.payload['target'];
    final type = log.payload['type'];
    if (target is! String || type is! String) {
      return [];
    } else {
      final properties = Map.of(log.payload)..remove('type');
      return [
        log.copyWith(
          tag: 'ga.action',
          payload: {
            'event_name': type,
            'properties': properties,
          },
        ),
        log.copyWith(
          tag: 'my.action',
          payload: {
            'event_name': '${type}_$target',
            'properties': Map.of(properties)..remove('target'),
          },
        ),
      ];
    }
  }
}

Output

Output does not provide buffering and retrying.

The following PrintOutput will output logs to the console.

class PrintOutput extends Output {
  PrintOutput({required super.tagPattern, super.shouldEmit});

  @override
  Future<void> emit(Log log) async {
    print('${log.loggedAt}:[${log.tag}] ${log.payload}');
  }
}

BufferedOutput

BufferedOutput provide buffering and retrying.

class MyLogOutput extends BufferedOutput {
  MyLogOutput({
    required super.tagPattern,
    super.flushInterval = 100,
    super.retryLimit = 3,
    super.logCountLimit = 5,
    LogStorage? logStorage,
  }) : super(logStorage: logStorage ?? LocalLogStorage());

  @override
  Future<bool> write(List<Log> logs) async {
    // TODO: send logs to your server.
    return Future<bool>.delayed(Duration(milliseconds: 50), () {
      logs.forEach((log) {
        final eventName = log.payload['event_name'];
        final properties = log.payload['properties'];
        print('๐Ÿฅ[MyLog] ${log.loggedAt}:[$eventName] $properties');
      });

      /// if return false, retrying.
      return true;
    });
  }
}

Make logger

Only Filter and Output that match TagPattern are used.

final logger = Logger(
  filters: [
    PageViewFilter(tagPattern: 'page'),
    ActionFilter(tagPattern: 'action'),
  ],
  outputs: [
    PrintOutput(tagPattern: '**'),
    MyLogOutput(tagPattern: 'my.**'),
    AnalyticsOutput(tagPattern: 'ga.**'),
  ],
);

Post log

logger.post({'name': 'page1'}, tag: 'page_view');
logger.post({'type': 'click', 'target': 'event_button'}, tag: 'action');

TagPattern matching.

expect(TagPattern('aa.bb.cc').match('aa.bb.cc'), true);
expect(TagPattern('aa.bb.cc').match('aa.bb.dd'), false);
expect(TagPattern('aa.bb.cc').match('aa.bb'), false);
expect(TagPattern('aa.bb.cc').match('aa.bb.cc.dd'), false);

expect(TagPattern('aa.bb.*').match('aa.bb.cc'), true);
expect(TagPattern('aa.bb.*').match('aa.bb.cc.dd'), false);
expect(TagPattern('aa.bb.*').match('aa.bb'), false);

expect(TagPattern('aa.**').match('aa.bb.cc'), true);
expect(TagPattern('aa.**').match('aa.bb.cc.dd'), true);
expect(TagPattern('aa.**').match('aa'), false);

expect(TagPattern('*').match('aa.bb'), false);
expect(TagPattern('*').match('aa'), true);
expect(TagPattern('*').match('bb'), true);

expect(TagPattern('**').match('aa.bb.cc'), true);
expect(TagPattern('**').match('aa.bb'), true);
expect(TagPattern('**').match('aa'), true);

log_calico's People

Contributors

miyakeryo avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

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.