<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def to_dict(self, labels=None):
 """Convert LogEvent object to a dictionary."""

        output = {}
        if labels is None:
            labels = ['line_str', 'split_tokens', 'datetime', 'operation',
                      'thread', 'namespace', 'nscanned', 'ntoreturn',
                      'nreturned', 'ninserted', 'nupdated', 'ndeleted',
                      'duration', 'r', 'w', 'numYields']

        for label in labels:
            value = getattr(self, label, None)
            if value is not None:
                output[label] = value

        return output