ISEPOS-2280 Enchanced and adjusted base loggers
This commit is contained in:
parent
9c654357c8
commit
039cfe081e
@ -34,10 +34,15 @@
|
|||||||
%
|
%
|
||||||
% delete() - Destructor method that closes the file identifier when the logger is deleted.
|
% delete() - Destructor method that closes the file identifier when the logger is deleted.
|
||||||
%
|
%
|
||||||
% Example usage:
|
% Example Usage:
|
||||||
% logger = base_logger.getInstance();
|
% logger = base_logger.getInstance();
|
||||||
% logger.info('This is an info message');
|
% logger.info('Some info')
|
||||||
% logger.error('This is an error message');
|
%
|
||||||
|
% try
|
||||||
|
% error('Simulated error for stack trace logging');
|
||||||
|
% catch err
|
||||||
|
% logger.error('An error occurred:', err);
|
||||||
|
% end
|
||||||
%
|
%
|
||||||
% See also: fopen, fclose, dbstack, fprintf
|
% See also: fopen, fclose, dbstack, fprintf
|
||||||
|
|
||||||
@ -58,14 +63,14 @@ classdef base_logger < handle
|
|||||||
|
|
||||||
methods(Access=private)
|
methods(Access=private)
|
||||||
function this = base_logger()
|
function this = base_logger()
|
||||||
this.fid = fopen(getenv("DEFAULT_LOG_PATH"), 'a');
|
this.fid = fopen(getenv("APP_LOG_FILE"), 'a');
|
||||||
if this.fid == -1
|
if this.fid == -1
|
||||||
error('Failed to open log file: application.log');
|
error('Failed to open log file');
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function log(this, level, message)
|
function log(this, level, varargin)
|
||||||
current_time = datetime('now', 'Format', 'yyyy-MM-dd HH:mm:ss');
|
current_time = datestr(now, 'yyyy-mm-dd HH:MM:SS');
|
||||||
stack = dbstack('-completenames');
|
stack = dbstack('-completenames');
|
||||||
if length(stack) > 2
|
if length(stack) > 2
|
||||||
script_name = stack(3).name;
|
script_name = stack(3).name;
|
||||||
@ -73,33 +78,48 @@ classdef base_logger < handle
|
|||||||
script_name = 'Unknown';
|
script_name = 'Unknown';
|
||||||
end
|
end
|
||||||
|
|
||||||
if this.fid ~= -1
|
message = "";
|
||||||
fprintf(this.fid, '%s %s %s %s\n', char(current_time), level, script_name, message);
|
for i = 1:numel(varargin)
|
||||||
else
|
if isa(varargin{i}, 'MException')
|
||||||
error('File identifier is invalid. Cannot write to log.');
|
message = sprintf('%sError: %s\n', message, varargin{i}.message);
|
||||||
|
for j = 1:length(varargin{i}.stack)
|
||||||
|
message = sprintf('%s at %s (line %d)\n', message, varargin{i}.stack(j).name, varargin{i}.stack(j).line);
|
||||||
end
|
end
|
||||||
|
elseif isnumeric(varargin{i}) || isenum(varargin{i}) || islogical(varargin{i})
|
||||||
|
message = strcat(message, string(varargin{i}));
|
||||||
|
elseif ischar(varargin{i}) || isstring(varargin{i})
|
||||||
|
message = strcat(message, varargin{i});
|
||||||
|
else
|
||||||
|
message = strcat(message, 'Unsupported data type');
|
||||||
|
end
|
||||||
|
if i < numel(varargin)
|
||||||
|
message = strcat(message, ", ");
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
fprintf(this.fid, '%s %s %s %s\n', current_time, level, script_name, message);
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
methods(Access=public)
|
methods(Access=public)
|
||||||
function trace(this, message)
|
function trace(this, varargin)
|
||||||
this.log("TRACE", message);
|
this.log('TRACE', varargin{:});
|
||||||
end
|
end
|
||||||
|
|
||||||
function debug(this, message)
|
function debug(this, varargin)
|
||||||
this.log("DEBUG", message);
|
this.log('DEBUG', varargin{:});
|
||||||
end
|
end
|
||||||
|
|
||||||
function info(this, message)
|
function info(this, varargin)
|
||||||
this.log("INFO", message);
|
this.log('INFO', varargin{:});
|
||||||
end
|
end
|
||||||
|
|
||||||
function warning(this, message)
|
function warning(this, varargin)
|
||||||
this.log("WARNING", message);
|
this.log('WARNING', varargin{:});
|
||||||
end
|
end
|
||||||
|
|
||||||
function error(this, message)
|
function error(this, varargin)
|
||||||
this.log("ERROR", message);
|
this.log('ERROR', varargin{:});
|
||||||
end
|
end
|
||||||
|
|
||||||
function delete(this)
|
function delete(this)
|
||||||
|
@ -34,19 +34,24 @@
|
|||||||
%
|
%
|
||||||
% delete() - Destructor method that closes the file identifier when the logger is deleted.
|
% delete() - Destructor method that closes the file identifier when the logger is deleted.
|
||||||
%
|
%
|
||||||
% Example usage:
|
% Example Usage:
|
||||||
% logger = base_logger.getInstance();
|
% logger = base_logger.getInstance();
|
||||||
% logger.info('This is an info message');
|
% logger.info('Some info')
|
||||||
% logger.error('This is an error message');
|
%
|
||||||
|
% try
|
||||||
|
% error('Simulated error for stack trace logging');
|
||||||
|
% catch err
|
||||||
|
% logger.error('An error occurred:', err);
|
||||||
|
% end
|
||||||
%
|
%
|
||||||
% See also: fopen, fclose, dbstack, fprintf
|
% See also: fopen, fclose, dbstack, fprintf
|
||||||
|
|
||||||
classdef base_logger < handle
|
classdef base_logger < handle
|
||||||
properties(Access=private)
|
properties
|
||||||
fid;
|
fid;
|
||||||
end
|
end
|
||||||
|
|
||||||
methods(Static, Access = public)
|
methods(Static)
|
||||||
function obj = getInstance()
|
function obj = getInstance()
|
||||||
persistent instance;
|
persistent instance;
|
||||||
if isempty(instance)
|
if isempty(instance)
|
||||||
@ -56,15 +61,15 @@ classdef base_logger < handle
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
methods(Access=private)
|
methods
|
||||||
function this = base_logger()
|
function this = base_logger()
|
||||||
this.fid = fopen(getenv("DEFAULT_LOG_PATH"), 'a');
|
this.fid = fopen(getenv("APP_LOG_FILE"), 'a');
|
||||||
if this.fid == -1
|
if this.fid == -1
|
||||||
error('Failed to open log file: application.log');
|
error('Failed to open log file');
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function log(this, level, message)
|
function log(this, level, varargin)
|
||||||
current_time = strftime('%Y-%m-%d %H:%M:%S', localtime(time()));
|
current_time = strftime('%Y-%m-%d %H:%M:%S', localtime(time()));
|
||||||
stack = dbstack('-completenames');
|
stack = dbstack('-completenames');
|
||||||
if length(stack) > 2
|
if length(stack) > 2
|
||||||
@ -73,33 +78,47 @@ classdef base_logger < handle
|
|||||||
script_name = 'Unknown';
|
script_name = 'Unknown';
|
||||||
end
|
end
|
||||||
|
|
||||||
if this.fid ~= -1
|
message = sprintf('');
|
||||||
fprintf(this.fid, '%s %s %s %s\n', char(current_time), level, script_name, message);
|
for i = 1:numel(varargin)
|
||||||
|
if isstruct(varargin{i}) && isfield(varargin{i}, 'message') && isfield(varargin{i}, 'stack')
|
||||||
|
error_info = varargin{i};
|
||||||
|
message = sprintf('%sError: %s\n', message, error_info.message);
|
||||||
|
for j = 1:length(error_info.stack)
|
||||||
|
message = sprintf('%s at %s (line %d)\n', message, error_info.stack(j).name, error_info.stack(j).line);
|
||||||
|
end
|
||||||
|
elseif isnumeric(varargin{i}) || islogical(varargin{i})
|
||||||
|
message = sprintf('%s%s', message, num2str(varargin{i}));
|
||||||
|
elseif ischar(varargin{i}) || isstring(varargin{i})
|
||||||
|
message = sprintf('%s%s', message, varargin{i});
|
||||||
else
|
else
|
||||||
error('File identifier is invalid. Cannot write to log.');
|
message = sprintf('%sUnsupported data type', message);
|
||||||
end
|
end
|
||||||
|
if i < numel(varargin)
|
||||||
|
message = sprintf('%s, ', message);
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
methods(Access=public)
|
fprintf(this.fid, '%s %s %s %s\n', current_time, level, script_name, message);
|
||||||
function trace(this, message)
|
|
||||||
this.log("TRACE", message);
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function debug(this, message)
|
function trace(this, varargin)
|
||||||
this.log("DEBUG", message);
|
this.log('TRACE', varargin{:});
|
||||||
end
|
end
|
||||||
|
|
||||||
function info(this, message)
|
function debug(this, varargin)
|
||||||
this.log("INFO", message);
|
this.log('DEBUG', varargin{:});
|
||||||
end
|
end
|
||||||
|
|
||||||
function warning(this, message)
|
function info(this, varargin)
|
||||||
this.log("WARNING", message);
|
this.log('INFO', varargin{:});
|
||||||
end
|
end
|
||||||
|
|
||||||
function error(this, message)
|
function warning(this, varargin)
|
||||||
this.log("ERROR", message);
|
this.log('WARNING', varargin{:});
|
||||||
|
end
|
||||||
|
|
||||||
|
function error(this, varargin)
|
||||||
|
this.log('ERROR', varargin{:});
|
||||||
end
|
end
|
||||||
|
|
||||||
function delete(this)
|
function delete(this)
|
||||||
|
@ -46,7 +46,7 @@ def getDefaultLogger(name):
|
|||||||
"""
|
"""
|
||||||
logger = logging.getLogger(name)
|
logger = logging.getLogger(name)
|
||||||
if not logger.hasHandlers():
|
if not logger.hasHandlers():
|
||||||
file_handler = logging.FileHandler(os.environ['DEFAULT_LOG_PATH'], mode='a')
|
file_handler = logging.FileHandler(os.environ['APP_LOG_FILE'], mode='a')
|
||||||
formatter = logging.Formatter('%(asctime)s,%(msecs)d %(levelname)s %(name)s %(message)s')
|
formatter = logging.Formatter('%(asctime)s,%(msecs)d %(levelname)s %(name)s %(message)s')
|
||||||
file_handler.setFormatter(formatter)
|
file_handler.setFormatter(formatter)
|
||||||
logger.addHandler(file_handler)
|
logger.addHandler(file_handler)
|
||||||
|
Loading…
Reference in New Issue
Block a user