1//
2// LogRotation.cpp
3//
4// This class demonstrates the Log rotation feature.
5//
6// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
7// and Contributors.
8//
9// SPDX-License-Identifier: BSL-1.0
10//
11
12
13#include "Poco/AutoPtr.h"
14#include "Poco/ConsoleChannel.h"
15#include "Poco/SplitterChannel.h"
16#include "Poco/FileChannel.h"
17#include "Poco/PatternFormatter.h"
18#include "Poco/FormattingChannel.h"
19#include "Poco/Message.h"
20#include "Poco/Logger.h"
21#include <iostream>
22#include <sstream>
23
24
25using Poco::AutoPtr;
26using Poco::Channel;
27using Poco::ConsoleChannel;
28using Poco::SplitterChannel;
29using Poco::FileChannel;
30using Poco::FormattingChannel;
31using Poco::Formatter;
32using Poco::PatternFormatter;
33using Poco::Logger;
34using Poco::Message;
35
36
37int main (int, char**)
38{
39 AutoPtr<SplitterChannel> splitterChannel(new SplitterChannel());
40
41 Channel::Ptr consoleChannel(new ConsoleChannel());
42 Channel::Ptr fileChannel(new FileChannel("test.log"));
43 AutoPtr<FileChannel> rotatedFileChannel(new FileChannel("rotated.log"));
44
45 rotatedFileChannel->setProperty("rotation", "100");
46 rotatedFileChannel->setProperty("archive", "timestamp");
47
48 splitterChannel->addChannel(consoleChannel);
49 splitterChannel->addChannel(fileChannel);
50 splitterChannel->addChannel(rotatedFileChannel);
51
52
53 AutoPtr<Formatter> formatter(new PatternFormatter("%h-%M-%S.%i: %t"));
54 Channel::Ptr formattingChannel(new FormattingChannel(formatter, splitterChannel));
55
56 Logger& logger = Logger::create("TestLog", formattingChannel, Message::PRIO_TRACE);
57
58 for (int i = 0; i < 10; ++i)
59 {
60 std::ostringstream oss;
61 oss << "Value of i: " << i;
62 logger.fatal(oss.str());
63 }
64
65 return 0;
66}
67