Giter VIP home page Giter VIP logo

mgs.logger's Introduction

[TOC]

MGS.Logger

Summary

  • Logger for C# project develop.
  • Logger for Unity project.

Platform

  • Windows
  • Android

Environment

  • Unity 5.0 or above.

  • .Net Framework 3.5 or above.

Demand

  • Output log to local file.
  • Implement custom logger to output the log that print by LogUtility from other module.

Usage

  • Reference

    If you need use LogUtility in dll:

    • Create a C# project, move the following files into the project:
      • ./Scripts/Interface/...
      • ./Scripts/FileLogger.cs
      • ./Scripts/LogUtility.cs
    • Compile project as dll to reference.
  • Use LogUtility to output log content.

//The log output at Unity console in editor mode;
//and write to local file at the path:
//string.Format("{0}/Log/", Application.persistentDataPath)

LogUtility.Log("Log info is {0}", info);
LogUtility.LogError("Log error is {0}", error);
LogUtility.LogWarning("Log warning is {0}", warning);
  • Wrap LogUtility specifically.
public sealed class Logger
{
    // A good way to use the LogUtility is wrap it specifically.
    // Example: add module prefix identification and more infos.

    public static void Log(string format, params object[] args)
    {
        LogUtility.Log(AddPrefix(format), args);
    }
    
    private static string AddPrefix(string origin)
    {
        return "[Demo] " + origin;
    }
}

Logger.Log("Log info is {0}", info);
  • Use log Filter.
//Implemente IFilter base your logic.
public class Filter : IFilter
{
    public bool Select(string tag, string format, params object[] args)
    {
        //TODO: Decide whether to select this log.
        return tag.Contains(FileLogger.TAG_ERROR);
    }
}

//Register logger to LogUtility.
var logDir = string.Format("{0}/Log/", Environment.CurrentDirectory);
LogUtility.Register(new FileLogger(logDir, new Filter()));
  • Override log path.
//Override the LogUtilityInitializer.Awake();
//new a FileLogger with custom log file path;
//Register the FileLogger to LogUtility.

var logDir = string.Format("{0}/Log/", Environment.CurrentDirectory);
LogUtility.Register(new FileLogger(logDir));
  • Override the Logger of LogUtility.
//Override the LogUtilityInitializer.Awake();
//Implemente a CustomLogger;
//Register the CustomLogger to LogUtility.

public class CustomLogger : ILogger
{
  public void Log(string format, params object[] args)
  {
    //TODO: Implemente the logic.
  }

  public void LogError(string format, params object[] args)
  {
    //TODO: Implemente the logic.
  }

  public void LogWarning(string format, params object[] args)
  {
    //TODO: Implemente the logic.
  }
}

LogUtility.Register(new CustomLogger());

Demo

  • Demos in the path "MGS.Packages/Logger/Demo/" provide reference to you.

Preview

Logger Output


Copyright © 2021 Mogoson. [email protected]

mgs.logger's People

Contributors

mogoson avatar

Watchers

James Cloos 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.