---
title: " <24>用error_logger间隔记录日志"
author: "ZacMa"
date: "2013-10-22T16:09:48Z"
tags: []
categories: ["编程语言"]
slug: "24-error-logger"
draft: false
lastmod: "2013-10-23T09:26:22Z"
id: 1962348
url: "https://ZacMa.iteye.com/blog/1962348"
---

执行下面的代码
test:start().
test.erl 
-module(test).
-compile(export_all).
start() ->
    Fun = loop,
    spawn(?MODULE, Fun, ).
loop() ->
    receive 
    after 5000 ->
        error_logger:delete_report_handler(error_logger_file_h),                                                        PDir = "/home/mzh/pratise", 
        {H, M, S} = time(),
        SDir = lists:flatten(io_lib:format('~2..0b~2..0b~2..0b', )),
        FileName = "log",
        Path = PDir ++ "/" ++ SDir,
        File = PDir ++ "/" ++ SDir ++ "/" ++ FileName,
        file:make_dir(Path),
        io:format("~n~p~n", ),
        error_logger:swap_handler({logfile, File}),
        error_logger:error_msg(File)
    end,
    loop().

每隔5秒钟把日志记录到以时间命名的目录中去，
-bash-3.2$ pwd
/home/mzh/pratise
-bash-3.2$ ls
154806  154816  154826  154836  154846  154856  154906  154916  154926  154936  154946  154956  155006  ...
每个文件夹中都是一个log文件，
log里面的内容都相同，是error_logger:error_msg(File)打印的内容,
例如其中一个log的内容
-bash-3.2$ cat 155011/log 

=ERROR REPORT==== 22-Oct-2013::15:50:11 ===
/home/mzh/pratise/155011/log,
当然日志记录用rb也非常方便，会自动分类,rb使用方法
<http://zacma.iteye.com/blog/1704685>