forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[C++] Add C++ single file logger factory (apache#10712)
* Add SampleFileLogger C++ example * Expose SingleFileLoggerFactory as the public interface * Make logger factory public * Change logger factory's name * Use PImpl idiom for LoggerFactory classes
- Loading branch information
1 parent
94c7301
commit b7aa794
Showing
17 changed files
with
280 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
#include <pulsar/Client.h> | ||
|
||
using namespace pulsar; | ||
|
||
int main(int argc, char* argv[]) { | ||
ClientConfiguration clientConf; | ||
// The logs whose level is >= INFO will be written to pulsar-cpp-client.log | ||
clientConf.setLogger(new FileLoggerFactory(Logger::Level::LEVEL_INFO, "pulsar-cpp-client.log")); | ||
|
||
Client client("pulsar://localhost:6650", clientConf); | ||
Producer producer; | ||
client.createProducer("my-topic", producer); // just to create some logs | ||
client.close(); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <pulsar/Logger.h> | ||
|
||
namespace pulsar { | ||
|
||
class FileLoggerFactoryImpl; | ||
|
||
/** | ||
* A logger factory that is appending logs to a single file. | ||
* | ||
* The log format is "yyyy-mm-dd hh:MM:ss.xxx <level> <thread-id> <file>:<line> | <msg>", like | ||
* | ||
* ``` | ||
* 2021-03-24 17:35:46.571 INFO [0x10a951e00] ConnectionPool:85 | Created connection for ... | ||
* ``` | ||
* | ||
* Example: | ||
* | ||
* ```c++ | ||
* #include <pulsar/FileLoggerFactory.h> | ||
* | ||
* ClientConfiguration conf; | ||
* conf.setLogger(new FileLoggerFactory(Logger::LEVEL_DEBUG, "pulsar-client-cpp.log")); | ||
* Client client("pulsar://localhost:6650", conf); | ||
* ``` | ||
*/ | ||
class PULSAR_PUBLIC FileLoggerFactory : public pulsar::LoggerFactory { | ||
public: | ||
/** | ||
* Create a FileLoggerFactory instance. | ||
* | ||
* @param level the log level | ||
* @param logFilePath the log file's path | ||
*/ | ||
FileLoggerFactory(Logger::Level level, const std::string& logFilePath); | ||
|
||
~FileLoggerFactory(); | ||
|
||
pulsar::Logger* getLogger(const std::string& filename) override; | ||
|
||
private: | ||
std::unique_ptr<FileLoggerFactoryImpl> impl_; | ||
}; | ||
|
||
} // namespace pulsar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
#include <pulsar/ConsoleLoggerFactory.h> | ||
#include "lib/ConsoleLoggerFactoryImpl.h" | ||
|
||
namespace pulsar { | ||
|
||
ConsoleLoggerFactory::ConsoleLoggerFactory(Logger::Level level) | ||
: impl_(new ConsoleLoggerFactoryImpl(level)) {} | ||
|
||
ConsoleLoggerFactory::~ConsoleLoggerFactory() {} | ||
|
||
Logger* ConsoleLoggerFactory::getLogger(const std::string& fileName) { return impl_->getLogger(fileName); } | ||
|
||
} // namespace pulsar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <pulsar/Logger.h> | ||
#include "lib/SimpleLogger.h" | ||
|
||
namespace pulsar { | ||
|
||
class ConsoleLoggerFactoryImpl { | ||
public: | ||
ConsoleLoggerFactoryImpl(Logger::Level level) : level_(level) {} | ||
|
||
Logger* getLogger(const std::string& fileName) { return new SimpleLogger(std::cout, fileName, level_); } | ||
|
||
private: | ||
Logger::Level level_; | ||
}; | ||
|
||
} // namespace pulsar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
#include <pulsar/FileLoggerFactory.h> | ||
#include "lib/FileLoggerFactoryImpl.h" | ||
|
||
namespace pulsar { | ||
|
||
FileLoggerFactory::FileLoggerFactory(Logger::Level level, const std::string& logFilePath) | ||
: impl_(new FileLoggerFactoryImpl(level, logFilePath)) {} | ||
|
||
FileLoggerFactory::~FileLoggerFactory() {} | ||
|
||
Logger* FileLoggerFactory::getLogger(const std::string& filename) { return impl_->getLogger(filename); } | ||
|
||
} // namespace pulsar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
#pragma once | ||
|
||
#include <fstream> | ||
#include <ios> | ||
#include <string> | ||
#include <pulsar/Logger.h> | ||
|
||
#include "lib/SimpleLogger.h" | ||
|
||
namespace pulsar { | ||
|
||
class FileLoggerFactoryImpl { | ||
public: | ||
FileLoggerFactoryImpl(Logger::Level level, const std::string& logFilePath) | ||
: level_(level), os_(logFilePath, std::ios_base::out | std::ios_base::app) {} | ||
|
||
~FileLoggerFactoryImpl() { os_.close(); } | ||
|
||
Logger* getLogger(const std::string& filename) { return new SimpleLogger(os_, filename, level_); } | ||
|
||
private: | ||
const Logger::Level level_; | ||
std::ofstream os_; | ||
}; | ||
|
||
} // namespace pulsar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.