保存日志/统计数据的最佳方式

发布于 2024-07-19 00:01:42 字数 325 浏览 2 评论 0原文

我使用 Catalyst(Perl 的 MVC 框架),但这个问题可能适用于所有 MVC 框架。

到目前为止,我使用 Apache 日志文件来获取有关访问者的统计信息:用户代理、访问的 URL、时间等。但现在我转向了 MVC 框架,我认为这还不够。 例如,如果对 /1/foo 和 /1/bar 的请求对我来说是相同的,我只想在我的日志中显示 /1/ 。

所以我想知道生成自己的统计日志文件的最佳方法是什么。 我应该将其视为应用程序中的另一个日志文件吗?

这些统计数据可以随时记录。 理想情况下,它们会在页面发送给用户之后被记录,因此不会感觉到记录所需的额外时间。

I use Catalyst (MVC framework for Perl), but the question probably apply to all MVC framework.

Until now, I used the Apache log files to get statistics about the visitors: user agent, URL accessed, time, etc. But now that I moved to an MVC framework, I don't think this is adequate. If a request to /1/foo and /1/bar are the same for me, I want to show /1/ in my log only, for example.

So I am wondering what is the best way to generate my own log files for statistics. Should I treat it as just another log file in my application?

These statistics can be logged at any time. Ideally they would be logged after the page is sent to the user, so it will not feel the additional time required for logging.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江挽川 2024-07-26 00:01:42

鉴于 Catalyst 已经广泛使用子例程属性,一种有用的方法可能是使用属性将相关子例程包装在自定义日志记录机制中。 我写了一篇关于这种技术的文章以日志记录为例。 基本思想是:

use Attribute::Handlers;

sub Log : ATTR(CODE) {
    my ($pkg, $sym, $code) = @_;


    my $name = *{ $sym }{NAME};

    no warnings 'redefine';

    *{ $sym } = sub {
        log_message("Entering sub $pkg\:\:$name");
        $code->( @_ );
    };
}

sub foo : Log { 
    # this will be logged
}

Given that Catalyst already uses subroutine attributes liberally, one useful approach might be to use an attribute to wrap the relevant subs in a custom logging mechanism. I wrote an article about this technique which uses logging as an example. The basic idea is:

use Attribute::Handlers;

sub Log : ATTR(CODE) {
    my ($pkg, $sym, $code) = @_;


    my $name = *{ $sym }{NAME};

    no warnings 'redefine';

    *{ $sym } = sub {
        log_message("Entering sub $pkg\:\:$name");
        $code->( @_ );
    };
}

sub foo : Log { 
    # this will be logged
}
纸短情长 2024-07-26 00:01:42

如果需要这种程度的灵活性,弗里多的回答非常漂亮。

OTOH,您可以继续使用 Apache error_log 来记录此数据,只需使用 $c->log->info() 或其同级之一。 扩展 Catalyst::Log 来报告其他类型的消息是相当简单的。 例如,我使用 $c->log->sql() 变体将 SQL 写入通过 SQL::Beautify 运行的 error_log。

我可以想象类似

sub auto {
    ...
    $c->log->audit(sprintf("%s called by %s", $c->action->reverse, $c->userid));
    ...
}

将其放在开头(在自动中)的东西不是您想要的,但它绝对没有问题,因为您知道它总是会被调用,与结束处理程序不同。

friedo's answer is incredibly nifty if that degree of flexibility is required.

OTOH you can continue to use the Apache error_log to record this data just by using $c->log->info() or one of its siblings. It's fairly trivial to extend Catalyst::Log to report other sorts of messages. I use a $c->log->sql() variant that writes SQL out to the error_log that's been run through SQL::Beautify, for example.

I can imagine something along the lines of

sub auto {
    ...
    $c->log->audit(sprintf("%s called by %s", $c->action->reverse, $c->userid));
    ...
}

Putting it at the start (in auto) is not what you wanted, but it's definitely less problematic, since you know it will always get called, unlike end handlers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文