CocoaLumberjack 的全局日志级别
我在 iPhone 项目中使用 CocoaLumberjack 来记录一些信息。
我已遵循入门指南,一切正常,但有一件事这让我很烦恼:似乎没有一种优雅的方法来定义整个应用程序的日志级别。为了使其工作,我需要在每个源文件中定义一个常量,如下所示:
static const int ddLogLevel = LOG_LEVEL_VERBOSE;
那么,有没有办法为应用程序定义全局日志级别?
我找到了关于这个主题的这篇文章,但是我仍然需要在每个文件中添加#import...
I'm using CocoaLumberjack in an iPhone project, to log some information.
I've followed the Getting started guide, and everything works fine, but there is one thing that bugs me: there doesn't seem to be an elegant way to define a log level for the whole app. To make it work I need to define a constant in every source file, like this:
static const int ddLogLevel = LOG_LEVEL_VERBOSE;
So, is there a way to define a global log level for the application?
I found this article on the subject, but I still need to add an #import in every file...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
您可以在 *.pch 文件中使用
#include
语句,以便它自动包含在所有项目的文件中。You could use an
#include
statement in your *.pch file so that it's automatically included in all your project's files.我没有找到比 我在问题中提到的文章。
Constant.h
Constant.m
记录器配置
导入您的类
I didn't find a better way to do it than the one explained in the article I mentioned in the question.
Constant.h
Constant.m
Logger configuration
Import your class
请不要再使用前缀标头了。
您不需要现在已弃用的
.pch
文件,只需在需要的地方包含一个头文件即可。Logger.h - CocoaLumberjack 1.9.x
Logger.m
对 CocoaLumberjack 2.x 的更改
如果 2.0 超出测试版时语法发生变化,请发表评论或编辑。
AppDelegate 中的用法示例
No more Prefix Headers, please.
You do not need the now deprecated
.pch
file, simply include a header file where needed.Logger.h - CocoaLumberjack 1.9.x
Logger.m
Changes for CocoaLumberjack 2.x
If the syntax changes when 2.0 is out of beta please comment or edit.
Example usage in AppDelegate
您可以在 *.pch 文件中使用它,根据您当前的构建配置自动获取不同的全局日志级别。[对于 xcode 4+]
或者如果您需要为每个记录器使用不同的日志级别,您可以使用 DDLog 轻松实现此目的+addLogger:withLogLevel: 方法。
在您提到的每个源文件中定义日志级别有一个好处。您可以仅对当前正在处理的部分使用详细日志记录级别。对于其余部分,您可以使用其他级别,如信息、警告、错误。
You can use this in your *.pch file to automatically get different global log levels depending upon your current build configuration.[for xcode 4+]
or If you need a different log level for every logger, you can easily achieve this using the DDLog +addLogger:withLogLevel: method.
Defining a log level in every source file you mentioned has a benefit. You can use verbose logging level just for the part that you're currently working on. For rest part, you can use other level like info, warn, error.
为了动态注入日志级别(例如,从配置文件):
1) 使用以下代码创建一个名为 DDLogLevel 的新类:
2) 在 DDLogLevel.h 中,找到包含以下语句的行:
并将其替换为:
3) 最后,从初始化过程(可能是从 appDelegate)调用所需级别的 ddSetLogLevel。
In order to dynamically inject log level (for example, from configuration file):
1) Create a new class named DDLogLevel with the following code:
2) In DDLogLevel.h, find the row that contains the following statement:
And replace it with:
3) Finally, call from your Initialization process (perhaps from appDelegate) to ddSetLogLevel with the desired level.
对于使用 CocoaLumberjackSwift 的用户,您只需在代码中的任何位置设置以下全局变量:
在这里讨论
For those using CocoaLumberjackSwift you can simply set the following global variable anywhere in your code:
Discussion here
分享我的 CocoaLumberjack 2.0.0 配置,其中包含
全局日志级别
和可选的本地日志级别
,并保留了 DynamicLogLevels 功能。我的解决方案包括简单的头文件
DSLogging.h
(及其对应文件),该文件导入CocoaLumberjack.h
并定义方便的宏来设置使用 CocoaLumberjack 日志宏的文件。以下是您应该如何使用它:DSLlogging.h
标头(两种方式):.pch
文件中导入一次。 在这样做之前请考虑这一点。DSLogLevelSetup...
设置文件日志级别的宏。注意:每个使用日志记录的源文件中都应该有宏。有关更多详细信息,请参阅内部文档。 下载要点。
DSLogging.h
标头:DSLogging.m
来源:为什么我认为这是一个好方法:
它比 CocoaLumberjack 好一点
它不会削减 CocoaLumberjack 功能
我是 CocoaLumberjack 的新手,我可能对自己的方法过于乐观,如果我在某个时候撒谎,我会很高兴听到您的批评。
Share my configuration for CocoaLumberjack 2.0.0 with
global log level
and optionallocal log level
with preserved DynamicLogLevels feature.My solution includes simple header file
DSLogging.h
(and it's counterpart) that importCocoaLumberjack.h
and define convenience macros for setting up the files that use CocoaLumberjack log macros. Here is how you should use it:DSLogging.h
header (two ways):.pch
file once. Consider this before going this way.DSLogLevelSetup...
macros to set log level for file. Note: there should be macros in EACH source file that uses logging.See documentation inside for more details. Download gist.
DSLogging.h
header:DSLogging.m
source:Why I think it's a good approach:
It's a little better than just CocoaLumberjack
It doesn't cut CocoaLumberjack functions
I'm new to CocoaLumberjack and I can be too optimistic about my approach, would be glad to hear your critics if I lie at some point.
正如 FreeAsInBeer 所回答的,您可以在 .pch 文件中定义此常量。您可以在 .pch 文件中这样做。
在我的工具中,我为自定义 Lumberjack 设置创建了一个新的头文件(例如 mylog.h)。这样,我在 .pch 文件中使用
#import
语句来包含 mylog.h。这个自定义头文件可能是这样的。As answered by FreeAsInBeer, you can define this constant in .pch file. You may do like this in .pch file.
Im my implement, I create a new header file(e.g. mylog.h) for custom Lumberjack settings. in this way, I use
#import
statement in my .pch file for including mylog.h. This custom header file may like this.我这样做的方式受到 this 答案的启发..但是,这就是我的不同做法,以便我可以同时拥有全局级别日志级别并且能够覆盖每个文件中的全局日志级别(如果我选择):
Constants.h
,而是将其称为GlobalDebugLevel.h
。这是因为在此文件中包含任何其他全局常量是没有意义的,除非您确实始终使用全局调试级别并且不使用文件特定的日志级别。static const int ddLogLevel = LOG_LEVEL_VERBOSE;
以及每个人很高兴:)
ps这是一个
.pch
免费解决方案。最初我尝试过,但是编译器会抱怨每当我想覆盖它时ddLogLevel
已经定义了文件级别The way I did it was inspired by this answer.. however This is how I did it differently so that I can have both a global level log level and be able to override the global log level within each file if I so chose:
Constants.h
I called itGlobalDebugLevel.h
. This is because it doesn't make sense to include any other global constants in this file, unless you really will always use the global debug level and have no use for file specific log levels.static const int ddLogLevel = LOG_LEVEL_VERBOSE;
and everybody is happy :)
p.s. this is a
.pch
free solution.. inititally I tried that but then the compiler would complain thatddLogLevel
is already defined whenever I wanted to override it at a file level有一种更简单的方法可以解决这个问题,您可以在 Logger 实例化时设置日志级别:
因此不需要额外的导入或 .pch 文件。
There is a much easier way to solve this, you can set the log level at the Logger instantiation:
So there is no need for extra imports or .pch-file.
这是一个动态日志记录示例,它使用下面的 DanSkeels DSLogging 代码:
GFDPerson.h
GFDPerson.m
main.m
此代码的输出:
请评论,如果我误解或误用了某些内容...
Here is a dynamic logging example, which uses DanSkeels DSLogging code from below:
GFDPerson.h
GFDPerson.m
main.m
output of this code:
Please comment, If I misunderstood or misused something...
CocoaLumberjack 附带了一个示例应用程序,展示了如何设置全局日志级别,您可以在此处找到该应用程序 https://github.com/robbiehanson/CocoaLumberjack/tree/master/Xcode/GlobalLogLevel
There's an example app included with CocoaLumberjack that shows how to set a global log level that you can find here https://github.com/robbiehanson/CocoaLumberjack/tree/master/Xcode/GlobalLogLevel