1 Star 0 Fork 0

此间凉汐 / LxLog

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Log简介

项目链接: https://gitee.com/liangxi1125/asyncLog.git

Log是一个简易的,基于C++20(环境至少为C++17)的日志系统。目前可以将日志输出到控制台和文件中。

主要功能在Logger类中,想要了解详细情况可以此类为切入点。

Logger类

日志系统把日志分为五个等级,如下:

 //日志类型
enum class LogLevel {
    Debug = 0,
    Info,
    Warning,
    Error,
    Fatal
};       

同时,有两种输出方式:

//输出方式
enum class Output {
    Console = 0,
    File
};

日志对象保证全局唯一,可以通过Logger::getInstance()方法获取该对象并使用。目前用于输出日志的方法只有一个:

virtual void asyncLog(std::string_view content, LogLevel level = LogLevel::Debug,
                       Output out = Output::Console, std::string_view path = "./Log/");

其中content是日志内容,level是日志等级,out是输出方式,path是输出路径。当向控制台输出时,后两个参数可以省略; 当向文件输出时,第三个参数必须修改,第四个可以使用默认路径,也可以修改。

因为向控制台或者向文件输出时,方法的实现有很大不同,这里为了以后扩展,因此使用策略模式。通过将asyncLog(输出)行为抽象出来并由子类 实现两种不同的输出(具体见代码)。

这样,Logger类只需要存有一个Write类的指针,就可以根据不同的输出方式,保存不同的实例来实现不同的输出:

//策略模式,实现算法替换
std::unique_ptr<Write> writeBehavior;

使用示例

  • 控制台输出:
Logger::getIntance().write("Debug3 of asyncLog to Console");
  • 文件输出:
Logger::getIntance().write("Debug3 of asyncLog to file", Logger::LogLevel::Debug, Logger::Output::File,"./Logs/");
MIT License Copyright (c) 2024 此间凉汐 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

简易log工具,可以库的形式加载到项目,也可以直接导入源码使用。 展开 收起
C++ 等 2 种语言
MIT
取消

贡献者

全部

近期动态

加载更多
不能加载更多了
C++
1
https://gitee.com/liangxi1125/LxLog.git
git@gitee.com:liangxi1125/LxLog.git
liangxi1125
LxLog
LxLog
master

搜索帮助